数据结构 - 链栈(C)
2014-11-24 22:07
253 查看
/* Stack.h - by Chimomo */ #ifndef Stack_H #define Stack_H typedef char Element; typedef struct node * PNode; /*Stack node*/ typedef struct node { int data; PNode next; } Node; /*Stack*/ typedef struct stack { PNode top; int size; } Stack; /*Construct an empty stack*/ Stack *InitStack(); /*Judge stack is empty or not*/ int IsEmpty(Stack *pStack); /*Push element into stack*/ PNode Push(Stack *pStack, Element element); /*Push element of integer into stack*/ PNode PushInt(Stack *pStack, int element); /*Pop element from stack*/ PNode Pop(Stack *pStack, Element *pElement); /*Return stack top element*/ PNode GetTop(Stack *pStack, Element *pElement); /*Return stack top element of integer*/ PNode GetTopInt(Stack *pStack, int *pElement); /*Return the size of stack*/ int GetSize(Stack *pStack); /*Traverse stack and call visit funciton for each stack element*/ void Traverse(Stack *pStack, void (*visit)(Element element)); /*Clear stack*/ void ClearStack(Stack *pStack); /*Destroy stack*/ void DestroyStack(Stack *pStack); #endif
/* Stack.c - by Chimomo */ #include<malloc h=""> #include"Stack.h" /*Construct an empty stack*/ Stack *InitStack() { Stack *pStack = (Stack *)malloc(sizeof(Stack)); if(pStack != NULL) { pStack->top = NULL; pStack->size = 0; } return pStack; } /*Judge stack is empty or not*/ int IsEmpty(Stack *pStack) { if(pStack->top == NULL && pStack->size == 0) { return 1; } else { return 0; } } /*Push element into stack*/ PNode Push(Stack *pStack, Element element) { PNode pnode = (PNode)malloc(sizeof(Node)); if(pnode != NULL) { pnode->data = element; pnode->next = GetTop(pStack, NULL); pStack->size++; pStack->top = pnode; } return pnode; } /*Push element of integer into stack*/ PNode PushInt(Stack *pStack, int element) { PNode pnode = (PNode)malloc(sizeof(Node)); if(pnode != NULL) { pnode->data = element; pnode->next = GetTop(pStack, NULL); pStack->size++; pStack->top = pnode; } return pnode; } /*Pop element from stack*/ PNode Pop(Stack *pStack, Element *pElement) { PNode p = pStack->top; if(!IsEmpty(pStack) && p != NULL) { if(pElement != NULL) { *pElement = p->data; } pStack->size--; pStack->top = pStack->top->next; free(p); } return pStack->top; } /*Return stack top element*/ PNode GetTop(Stack *pStack, Element *pElement) { if(!IsEmpty(pStack) && pElement != NULL) { *pElement = pStack->top->data; } return pStack->top; } /*Return stack top element of integer*/ PNode GetTopInt(Stack *pStack, int *pElement) { if(!IsEmpty(pStack) && pElement != NULL) { *pElement = pStack->top->data; } return pStack->top; } /*Return the size of stack*/ int GetSize(Stack *pStack) { return pStack->size; } /*Traverse stack and call visit funciton for each stack element*/ void Traverse(Stack *pStack, void (*visit)(Element element)) { PNode p = pStack->top; int i = pStack->size; while(i--) { visit(p->data); p = p->next; } } /*Clear stack*/ void ClearStack(Stack *pStack) { while(!IsEmpty(pStack)) { Pop(pStack, NULL); } } /*Destroy stack*/ void DestroyStack(Stack *pStack) { if(!IsEmpty(pStack)) { ClearStack(pStack); } free(pStack); } </malloc>
/* Main.c - by Chimomo */ #include<stdio.h> #include"Stack.h" void print(Element element) { printf("The node is %c\n", element); } main() { Stack *pStack = InitStack(); char i, element; printf("Push A-Z into stack:\n"); for(i = 'A'; i <= 'Z'; i++) { Push(pStack, i); GetTop(pStack, &element); printf("%c ", element); } printf("\nTraverse from stack top to bottom and call print function:\n"); Traverse(pStack, print); printf("Pop each element of stack:\n"); for(i = 'A'; i <= 'Z'; i++) { Pop(pStack, &element); printf("%c ", element); } ClearStack(pStack); if(IsEmpty(pStack)) { printf("\nStack was successfully cleared.\n"); } DestroyStack(pStack); printf("Stack was destroyed.\n"); } /* Output: Push A-Z into stack: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Traverse from stack top to bottom and call print function: The node is Z The node is Y The node is X The node is W The node is V The node is U The node is T The node is S The node is R The node is Q The node is P The node is O The node is N The node is M The node is L The node is K The node is J The node is I The node is H The node is G The node is F The node is E The node is D The node is C The node is B The node is A Pop each element of stack: Z Y X W V U T S R Q P O N M L K J I H G F E D C B A Stack was successfully cleared. Stack was destroyed. Press any key to continue . . . */
相关文章推荐
- 【数据结构】之链栈的java实现
- 【数据结构】迷宫问题求解(链栈,DFS)
- 数据结构的链栈基本操作
- 数据结构|链栈的实现(实验3.2)
- 数据结构_链栈
- Java数据结构-线性表之栈(顺序栈和链栈)
- 数据结构 第6讲 链栈
- 数据结构-链栈(C语言)
- [数据结构]c语言实现链栈的入栈,出栈,清空,销毁等操作
- 【数据结构】链栈的基本操作
- 数据结构-链栈
- Java数据结构-线性表之栈(顺序栈和链栈)
- 【C语言数据结构】链栈
- 数据结构---链栈的基本操作
- java与数据结构(6)---java实现链栈
- 数据结构学习(九)——链栈的操作
- 数据结构C语言实现之链栈
- C数据结构-链栈
- 数据结构:链栈
- 数据结构中栈的链式结构—链栈的基本操作