前言:栈的特点:先进先出,只在栈顶进行操作。栈低密封,不进行操作,栈的实现有两种方式,通过对线性表实现进行复用。安全性高。
实现栈的方式:
第一种:以顺序结构的方式实现:将顺序表的队尾作为栈顶
第二种:以链式结构的方式实现:将链式表的队头作为栈顶
第一种实现方式(顺序结构):
1、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
2、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;
}
3、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); //stack确定哪个栈,item地址
void* SeqStack_Pop(SeqStack* stack);
void* SeqStack_Top(SeqStack* stack);
int SeqStack_Size(SeqStack* stack);
int SeqStack_Capacity(SeqStack* stack);
#endif
4、SeqStack.c
#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)); //stack为栈的地址,item为元素的地址,seqList_Length从队尾插入
}
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_Size(SeqStack* stack) //获取栈的元素个数
{
return SeqList_Length(stack);
}
int SeqStack_Capacity(SeqStack* stack) //获取栈的内存容量
{
return SeqList_Capacity(stack);
}
5、main.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqStack.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
SeqStack* stack = SeqStack_Create(20); //创建一个有20元素大小的栈
int a[10];
int i = 0;
for(i=0; i<10; i++) //依次将10个元素压入栈中
{
a[i] = i;
SeqStack_Push(stack, a + i);
}
printf("Top: %d\n", *(int*)SeqStack_Top(stack));
printf("Capacity: %d\n", SeqStack_Capacity(stack));
printf("Length: %d\n", SeqStack_Size(stack));
while( SeqStack_Size(stack) > 0 ) //依次输出出栈的元素
{
printf("Pop: %d\n", *(int*)SeqStack_Pop(stack));
}
SeqStack_Destroy(stack);
return 0;
}
第二种方式(链式方式)
1、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
2、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;
}
3、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
4、LinkStack.c
#include <malloc.h>
#include "LinkStack.h"
#include "LinkList.h"
typedef struct _tag_LinkStackNode
{
LinkListNode header;
void* item;
} TLinkStackNode;
LinkStack* LinkStack_Create() //创建栈
{
return LinkList_Create();
}
void LinkStack_Destroy(LinkStack* stack) //销毁栈
{
LinkStack_Clear(stack);
LinkList_Destroy(stack);
}
void LinkStack_Clear(LinkStack* stack) //清空栈
{
while( LinkStack_Size(stack) > 0 ) //判断栈中是否有元素,若有则执行出栈动作
{
LinkStack_Pop(stack);
}
}
int LinkStack_Push(LinkStack* stack, void* item) //进栈 ,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);
}
5、main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkStack.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
LinkStack* stack = LinkStack_Create();
int a[10];
int i = 0;
for(i=0; i<10; i++)
{
a[i] = i;
LinkStack_Push(stack, a + i); //a+i为压入栈中元素的地址
}
printf("Top: %d\n", *(int*)LinkStack_Top(stack));
printf("Length: %d\n", LinkStack_Size(stack));
while( LinkStack_Size(stack) > 0 )
{
printf("Pop: %d\n", *(int*)LinkStack_Pop(stack));
}
LinkStack_Destroy(stack);
return 0;
}
原文地址:http://blog.csdn.net/liuchuangjin/article/details/46010321