数据结构:堆栈的链式存储实现
2015-04-13 14:14
489 查看
栈的链式存储实际是一个单链表,叫做栈链。插入和删除应该都只在栈链的栈顶进行。
#include<stdio.h> #include<stdlib.h> struct Node; typedef struct Node * PtrToNode; typedef PtrToNode Stack; struct Node{ ElementType element; PtrToNode Next; }; //创建栈 Stack createStack(){ Stack s; s = (PtrToNode)malloc(sizeof(PtrToNode)); if(s == NULL){ printf("fail"); s->Next = NULL; } } //检测栈是否为空 int isEmpty(Stack s){ return s->Next == NULL; } //使栈为空 void makeEmpty(Stack s){ Stack p,temp; p = s->Next; s->Next = NULL; while(p!=NULL){ temp = p->Next; free(p); p = temp; } } //入栈 void Push(ElementType ele,Stack s){ Stack tempCell; tempCell = (PtrToNode)malloc(sizeof(PtrToNode)); if(tempCell == NULL){ tempCell->element = ele; tempCell->Next = s->Next; s->Next = tempCell; } } //返回栈顶元素的值 ElementType Top(Stack s){ if(!isEmpty(s)){ return s->Next->element; }else{ Error("Empty Stack\n"); return 0; } } //出栈 void Pop(Stack s){ Stack firstCell; if(!isEmpty(s)){ firstCell = s->Next; s->Next = s->Next->Next; free(firstCell); }else{ Error("Empty"); } }
相关文章推荐
- 数据结构学习之堆栈的链式存储C++实现
- 数据结构学习笔记-栈的链式存储(C语言实现)
- 数据结构复习——线性表的链式存储实现(单向链表)
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- 线性表的链式存储实现(无头结点)(陈越数据结构)
- 数据结构——单向链表的链式存储及实现
- 重学数据结构003——栈的基本操作及实现(链式存储)
- LinkedList实现堆栈数据结构的存储方式与队列的数据结构存储方式
- 数据结构复习——线性表的链式存储实现(双向链表)
- 堆栈的链式存储的C++实现与测试
- 数据结构:线性表的链式存储(单向链表)--Java实现
- 数据结构:二叉树的链式存储实现
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- 堆栈的链式存储实现
- JAVA版数据结构之栈的链式存储实现
- 数据结构学习笔记-线性表链式存储(C语言实现)
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- 数据结构学习笔记-队列的链式存储(C语言实现)
- 2.2.3 堆栈的链式存储实现
- 数据结构学习之堆栈(链式存储)