1>定义:
栈是一种特殊的线性表;栈仅能在线性表的一段进行操作;
栈顶(Top):允许操作的一端
栈底(Bottom):不允许操作的一端
2>性质:后进先出
栈的常用操作有:创建栈,销毁栈,清空栈,进栈,出栈,获取栈顶元素,获取栈的大小
3>栈的顺序存储实现
-
/*SeqList.h*/
-
/************************************************************/
-
#ifndef _SEQLIST_H_
-
#define _SEQLIST_H_
-
-
typedef void SeqList;
-
typedef void SeqListNode;
-
-
SeqList* SeqList_Create(int capacity);
-
-
void SeqList_Destroy(SeqList* list);
-
-
void SeqList_Clear(SeqList* list);
-
-
int SeqList_Length(SeqList* list);
-
-
int SeqList_Capacity(SeqList* list);
-
-
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
-
-
SeqListNode* SeqList_Get(SeqList* list, int pos);
-
-
SeqListNode* SeqList_Delete(SeqList* list, int pos);
-
-
#endif
-
-
/*SeqList.c*/
-
/**************************************************************/
-
-
#include <stdio.h>
-
#include <malloc.h>
-
#include "SeqList.h"
-
-
typedef unsigned int TSeqListNode;
-
-
typedef struct _tag_SeqList
-
{
-
int capacity;
-
int length;
-
TSeqListNode* node;
-
} TSeqList;
-
-
SeqList* SeqList_Create(int capacity) // O(1)
-
{
-
TSeqList* ret = NULL;
-
-
if( capacity >= 0 )
-
{
-
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
-
}
-
-
if( ret != NULL )
-
{
-
ret->capacity = capacity;
-
ret->length = 0;
-
ret->node = (TSeqListNode*)(ret + 1);
-
}
-
-
return ret;
-
}
-
-
void SeqList_Destroy(SeqList* list) // O(1)
-
{
-
free(list);
-
}
-
-
void SeqList_Clear(SeqList* list) // O(1)
-
{
-
TSeqList* sList = (TSeqList*)list;
-
-
if( sList != NULL )
-
{
-
sList->length = 0;
-
}
-
}
-
-
int SeqList_Length(SeqList* list) // O(1)
-
{
-
TSeqList* sList = (TSeqList*)list;
-
int ret = -1;
-
-
if( sList != NULL )
-
{
-
ret = sList->length;
-
}
-
-
return ret;
-
}
-
-
int SeqList_Capacity(SeqList* list) // O(1)
-
{
-
TSeqList* sList = (TSeqList*)list;
-
int ret = -1;
-
-
if( sList != NULL )
-
{
-
ret = sList->capacity;
-
}
-
-
return ret;
-
}
-
-
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n)
-
{
-
TSeqList* sList = (TSeqList*)list;
-
int ret = (sList != NULL);
-
int i = 0;
-
-
ret = ret && (sList->length + 1 <= sList->capacity);
-
ret = ret && (0 <= pos);
-
-
if( ret )
-
{
-
if( pos >= sList->length )
-
{
-
pos = sList->length;
-
}
-
-
for(i=sList->length; i>pos; i--)
-
{
-
sList->node[i] = sList->node[i-1];
-
}
-
-
sList->node[i] = (TSeqListNode)node;
-
-
sList->length++;
-
}
-
-
return ret;
-
}
-
-
SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1)
-
{
-
TSeqList* sList = (TSeqList*)list;
-
SeqListNode* ret = NULL;
-
-
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
-
{
-
ret = (SeqListNode*)(sList->node[pos]);
-
}
-
-
return ret;
-
}
-
-
SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n)
-
{
-
TSeqList* sList = (TSeqList*)list;
-
SeqListNode* ret = SeqList_Get(list, pos);
-
int i = 0;
-
-
if( ret != NULL )
-
{
-
for(i=pos+1; i<sList->length; i++)
-
{
-
sList->node[i-1] = sList->node[i];
-
}
-
-
sList->length--;
-
}
-
-
return ret;
-
}
-
-
/*SeqStack.h*/
-
/**********************************************************/
-
-
#ifndef _SEQSTACK_H_
-
#define _SEQSTACK_H_
-
-
typedef void SeqStack;
-
-
SeqStack* SeqStack_Create(int capacity);
-
-
void SeqStack_Destroy(SeqStack* stack);
-
-
void SeqStack_Clear(SeqStack* stack);
-
-
int SeqStack_Push(SeqStack* stack, void* item);
-
-
void *SeqStack_Pop(SeqStack* stack);
-
-
void *SeqStack_Top(SeqStack* stack);
-
-
int SeqStack_Length(SeqStack* stack);
-
-
int SeqStack_Capacity(SeqStack* stack);
-
-
#endif
-
-
/*SeqStack.c*/
-
**************************************************************************
-
-
#include <stdio.h>
-
#include "SeqStack.h"
-
#include "SeqList.h"
-
-
SeqStack* SeqStack_Create(int capacity)
-
{
-
return SeqList_Create(capacity);
-
}
-
-
void SeqStack_Destroy(SeqStack* stack)
-
{
-
SeqList_Destroy(stack);
-
}
-
-
void SeqStack_Clear(SeqStack* stack)
-
{
-
SeqList_Clear(stack);
-
}
-
-
int SeqStack_Push(SeqStack* stack, void* item)
-
{
-
return SeqList_Insert(stack,item,SeqList_Length(stack));
-
}
-
-
void *SeqStack_Pop(SeqStack* stack)
-
{
-
return SeqList_Delete(stack,SeqList_Length(stack)-1);
-
}
-
-
void *SeqStack_Top(SeqStack* stack)
-
{
-
return SeqList_Get(stack,SeqList_Length(stack)-1);
-
}
-
-
int SeqStack_Length(SeqStack* stack)
-
{
-
return SeqList_Length(stack);
-
}
-
-
int SeqStack_Capacity(SeqStack* stack)
-
{
-
return SeqList_Capacity(stack);
-
}
4>
栈的链表实现方式(代码复用的思想)
-
/**********************LinkList.h******************************/
-
-
#ifndef _LINKLIST_H_
-
#define _LINKLIST_H_
-
-
typedef void LinkList;
-
typedef struct _tag_LinkListNode LinkListNode;
-
struct _tag_LinkListNode
-
{
-
LinkListNode* next;
-
};
-
-
LinkList* LinkList_Create();
-
-
void LinkList_Destroy(LinkList* list);
-
-
void LinkList_Clear(LinkList* list);
-
-
int LinkList_Length(LinkList* list);
-
-
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);
-
-
LinkListNode* LinkList_Get(LinkList* list, int pos);
-
-
LinkListNode* LinkList_Delete(LinkList* list, int pos);
-
-
#endif
-
-
/*********************LinkList.c***************************/
-
#include <stdio.h>
-
#include <malloc.h>
-
#include "LinkList.h"
-
-
typedef struct _tag_LinkList
-
{
-
LinkListNode header;
-
int length;
-
} TLinkList;
-
-
LinkList* LinkList_Create() // O(1)
-
{
-
TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
-
-
if( ret != NULL )
-
{
-
ret->length = 0;
-
ret->header.next = NULL;
-
}
-
-
return ret;
-
}
-
-
void LinkList_Destroy(LinkList* list) // O(1)
-
{
-
free(list);
-
}
-
-
void LinkList_Clear(LinkList* list) // O(1)
-
{
-
TLinkList* sList = (TLinkList*)list;
-
-
if( sList != NULL )
-
{
-
sList->length = 0;
-
sList->header.next = NULL;
-
}
-
}
-
-
int LinkList_Length(LinkList* list) // O(1)
-
{
-
TLinkList* sList = (TLinkList*)list;
-
int ret = -1;
-
-
if( sList != NULL )
-
{
-
ret = sList->length;
-
}
-
-
return ret;
-
}
-
-
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)
-
{
-
TLinkList* sList = (TLinkList*)list;
-
int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
-
int i = 0;
-
-
if( ret )
-
{
-
LinkListNode* current = (LinkListNode*)sList;
-
-
for(i=0; (i<pos) && (current->next != NULL); i++)
-
{
-
current = current->next;
-
}
-
-
node->next = current->next;
-
current->next = node;
-
-
sList->length++;
-
}
-
-
return ret;
-
}
-
-
LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)
-
{
-
TLinkList* sList = (TLinkList*)list;
-
LinkListNode* ret = NULL;
-
int i = 0;
-
-
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
-
{
-
LinkListNode* current = (LinkListNode*)sList;
-
-
for(i=0; i<pos; i++)
-
{
-
current = current->next;
-
}
-
-
ret = current->next;
-
}
-
-
return ret;
-
}
-
-
LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)
-
{
-
TLinkList* sList = (TLinkList*)list;
-
LinkListNode* ret = NULL;
-
int i = 0;
-
-
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
-
{
-
LinkListNode* current = (LinkListNode*)sList;
-
-
for(i=0; i<pos; i++)
-
{
-
current = current->next;
-
}
-
-
ret = current->next;
-
current->next = ret->next;
-
-
sList->length--;
-
}
-
-
return ret;
-
}
-
-
-
/*LinkStack.h*/
-
/**************************************************************/
-
#ifndef _LINKSTACK_H_
-
#define _LINKSTACK_H_
-
-
typedef void LinkStack;
-
-
LinkStack* LinkStack_Create();
-
-
void LinkStack_Destroy(LinkStack* stack);
-
-
void LinkStack_Clear(LinkStack* stack);
-
-
int LinkStack_Push(LinkStack* stack, void* item);
-
-
void *LinkStack_Pop(LinkStack* stack);
-
-
void *LinkStack_Top(LinkStack* stack);
-
-
int LinkStack_Size(LinkStack* stack);
-
-
#endif
-
-
/*LinkStack.c*/
-
/*************************************************************/
-
#include <malloc.h>
-
#include "LinkList.h"
-
#include "LinkStack.h"
-
-
typedef struct _tag_LinkStackNode
-
{
-
LinkListNode header;
-
void* item;
-
}TLinkStackNode;
-
-
LinkStack* LinkStack_Create()
-
{
-
return LinkList_Create();
-
}
-
-
void LinkStack_Destroy(LinkStack* stack)
-
{
-
-
}
-
-
void LinkStack_Clear(LinkStack* stack)
-
{
-
-
}
-
-
int LinkStack_Push(LinkStack* stack, void* item)
-
{
-
TLinkStackNode* node = (TLinkStackNode*)malloc(sizeof(TLinkStackNode));
-
int ret = (node != NULL) && (item != NULL);
-
-
if( ret )
-
{
-
node->item = item;
-
-
ret = LinkList_Insert(stack,(LinkListNode*)node,0);
-
}
-
-
if( !ret )
-
{
-
free(node);
-
-
}
-
-
return ret;
-
}
-
-
void *LinkStack_Pop(LinkStack* stack)
-
{
-
TLinkStackNode* node = (TLinkStackNode*)LinkList_Delete(stack, 0);
-
void* ret = NULL;
-
-
if( node != NULL)
-
{
-
ret = node->item;
-
-
free(node);
-
}
-
-
return ret;
-
}
-
-
void *LinkStack_Top(LinkStack* stack)
-
{
-
TLinkStackNode* node = (TLinkStackNode*)LinkList_Get(stack, 0);
-
void* ret = NULL;
-
-
if( node != NULL)
-
{
-
ret = node->item;
-
-
}
-
-
return ret;
-
-
}
-
-
int LinkStack_Size(LinkStack* stack)
-
{
-
return LinkList_Length(stack);
-
}
阅读(1382) | 评论(0) | 转发(0) |