栈模型的实现--链表实现
来源:互联网 发布:咸鱼不退不换淘宝介入 编辑:程序博客网 时间:2024/05/22 09:36
栈是限制插入和删除只能在一个位置上进行的表,该位置是表的末端,叫做栈顶(top)。push进栈相当于插入,pop相当于删除最后插入的元素,一般不对空栈进行pop和top操作,还有一个,push的时候空间用尽是一个实现错误.
/*栈的实现,包括对栈实现初始化,插入栈顶元素,删除栈顶元素,遍历栈,清空栈等基本操作*/#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>#include<Windows.h>#define STACK_SIZE 6void Empty(struct Stack *temp);/*创建一个空栈的辅助函数*/struct Stack *CreateStack(void);/*创建一个空栈*/int IsEmpty(struct Stack *S);/*测试栈是否为空*/void Push( struct Stack*sp); /*进栈例程*/int Top(struct Stack*temp); /*返回栈顶元素*/void Pop(struct Stack*temp); /*从栈弹出元素*/void Rebuild(struct Stack *p); /*释放栈空间*/void Print(); /*菜单打印*/void Exit(); /*退出*/struct Stack { /*栈的声明*/ int Element; struct Stack *next;};struct Stack *stack = NULL;int main(void) { char ch = '\0'; while (1) { system("cls"); fflush(stdin); Print(); scanf(" %c",&ch); switch (ch) { case'a': stack=CreateStack(); break; case'b': Push(stack); break; case'c': Top(stack); break; case'd': Pop(stack); break; case'e': Rebuild(stack); break; case'f': Exit(); break; default: printf("无此选择项!\n"); system("pause"); break; } } return 0;}void Rebuild(struct Stack *p) { if (p==NULL) { printf("Must use CreateStack first!"); return; } struct Stack *temp = NULL; struct Stack *tt = NULL; temp = p; p->next = NULL; while (temp != NULL) { tt = temp->next; free(temp); temp = tt; } puts("Rebuild!\n"); system("pause");}void Exit() { /*这个是用来退出*/ int i = 0; printf("退出中"); for (i = 4; i > 0; --i) { Sleep(200); printf("."); } exit(0);}void Print(){ printf("-----主菜单功能如下:\n"); printf("-----a.创建一个栈\n"); printf("-----b.Push进栈\n"); printf("-----c.Top返回栈顶元素\n"); printf("-----d.Pop出栈\n"); printf("-----e.释放栈空间\n"); printf("-----f.退出\n");}int IsEmpty(struct Stack *S) { return S->next == NULL?1:0;}struct Stack *CreateStack(void){ struct Stack *temp=NULL; struct Stack *current = NULL; struct Stack *last = NULL; int size = STACK_SIZE; int a = 0; temp = (struct Stack*)malloc(sizeof(struct Stack)); while (size-- > 0) { current = (struct Stack*)malloc(sizeof(struct Stack)); if (temp == NULL) temp = current; if (last != NULL) last->next = current; current->next = NULL; last = current; } puts("Created!"); return (temp);}void Empty(struct Stack *temp) { if (temp == NULL) { printf("Must use CreateStack first!"); return; } else temp->next = NULL;}void Push( struct Stack*sp){ int x=0; struct Stack *temp = NULL; temp = (struct Stack*)malloc(sizeof(struct Stack)); if (temp == NULL) { printf( "out of space!"); } else { printf("Please enter a Element x!\n"); scanf("%d",&x); temp->Element = x; temp->next = sp->next; sp->next = temp; }}int Top(struct Stack*temp) { if (!IsEmpty(temp)) return temp->next->Element; printf( "Empty stack"); return 0; }void Pop(struct Stack*temp) { struct Stack *first = NULL; if(IsEmpty(temp)) printf("Empty stack"); else { first = temp->next; temp->next = temp->next->next; free(first); }}
阅读全文
0 0
- 栈模型的实现--链表实现
- WSAEventSelect 模型的实现
- select模型的实现
- 线程的实现模型
- 【C/C++】List链表的功能实现模型
- 实现模型
- Select模型的简单实现
- 服务器Select模型的实现
- 通用的分页模型实现.
- CityEngine2012实现模型的优化
- 生产者消费者模型的实现
- 云模型的MATLAB实现
- 高尔顿钉板概率模型的实现
- lda模型的python实现
- 一种Future模型的实现
- epoll模型的实现原理
- 生产者消费者模型的实现
- vgg16测试模型的实现
- 资源分享 | 平面设计 | 平面设计相关软件快捷键汇总
- URLConnection 和 HttpURLConnection
- 题目:画图,学用rectangle画方形
- 线性非时变系统理论(LTI系统理论)
- php中钩子(hook)的应用示例演示与下载
- 栈模型的实现--链表实现
- 分享给大家一个比较装逼的小代码
- 软件开发三原则,太经典
- 《UNIX网络编程 卷1》 笔记: UDP应用实现超时重传机制
- 《Android源码解析录》开篇
- java中字节数组转换为字符串
- 推荐一款功能强大的web弹框插件layer
- 编写MakeFile
- POJ1753-Flip Game