标签:
栈:后进先出(LIFO) last in first out
栈是一种特殊的线性表,只能在线性表的一端进行操作。
栈顶 top
栈底 bottom
实现方式:顺序结构实现,线性结构实现
链式存储实现
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 "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) { 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); }
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); } 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; }
顺序存储实现:
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_Size(SeqStack* stack); int SeqStack_Capacity(SeqStack* stack); #endif
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)); } 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); }
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); int a[10]; int i = 0; for(i=0; i<10; i++) { 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; }
标签:
原文地址:http://www.cnblogs.com/siqi/p/4823112.html