链式堆栈的实现 (带头节点的链式堆栈)
2010-09-27 17:47
218 查看
//-----------链式堆栈实现------------------- // // 实现的为 带头节点的链式堆栈 // // //------------2010年9月27日 17:32:42-------- By AndGod #include "stdio.h" #include "malloc.h" typedef int DataType ; //定义堆栈存储数据类型 typedef struct snode //定义链式堆栈节点 { DataType data; struct snode *next; }LSNode; void StackInitiate(LSNode **head) { //初始化带头节点的 链式堆栈 *head=(LSNode *)(malloc(sizeof(LSNode))); (*head)->next=NULL; } int StackIsEmpty(LSNode *head) { //判断链式堆栈是否为空 --空则返回1,非空则返回0 if(head->next == NULL) { return 1; } else { return 0; } } void StackPush(LSNode *head,DataType x) { //进栈------------ LSNode *p; p=(LSNode*)malloc(sizeof(LSNode)); p->data=x; p->next=head->next; head->next=p; } int StackPop(LSNode *head,DataType *x) { //出栈, 元素由x带出,成功返回 1 失败返回0 if(StackIsEmpty(head)) { return 0; } else { *x=(head->next)->data; head->next=(head->next)->next; return 1; } } int StackTop(LSNode *head,DataType *x) { //查看栈顶元素, 元素由x带出,成功返回 1 失败返回0 if(StackIsEmpty(head)) { return 0; } else { *x=head->data; return 1; } } //测试函数----------- void main() { LSNode *head; DataType x; StackInitiate(&head); for(int i=0; i<20; i++) { StackPush(head,i); } for(i=0;i<21;i++) { if(StackPop(head,&x)) { printf("出栈%d/n",x); } else { printf("堆栈已空!/n"); break; } } }
相关文章推荐
- 带头节点的单链表的实现
- 链式堆栈_C语言实现
- 链式堆栈的实现
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- 堆栈之链式实现
- 数据结构(十六) 线索二叉树讲解(带头节点后序加线索) 以及实现的完整代码
- 带头节点的单链表及其基本操作(Java实现)
- 简单顺序栈和链式堆栈的功能实现
- 【c++版数据结构】之双链表的实现(带头结点以及尾节点)
- 数据结构:堆栈的链式存储实现
- 不带头节点的单链表及其基本操作(Java实现)
- 2.2.3 堆栈的链式存储实现
- C/C++ 堆栈、队列的链式实现
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- c实现按访问频度非递增有序的带头节点的双向链表检索关键字
- 单链表C语言实现(带头节点)
- 堆栈链式实现入栈出栈等操作(C)
- 堆栈的链式表示和实现
- 带头节点单链表的所有操作(目前我所想到的),linux纯C实现
- 堆栈的链式存储实现