数据结构(4):链栈的表示和实现
来源:互联网 发布:2017年淘宝流量暴跌 编辑:程序博客网 时间:2024/05/08 06:17
/* 语言:C++ 编译环境:Visual C++6.0 链栈的表示和实现*/#include <iostream>#define OK 1#define ERROR 0#define OVERFLOW -2// Status是函数返回值类型,其值是函数结果状态代码typedef int Status;// 自定义数据类型别名typedef int ElemType;using namespace std;// 链栈的存储结构typedef struct StackNode{ ElemType data; struct StackNode *next;}StackNode,*LinkStack;// 链栈的初始化Status InitStack(LinkStack &S){ // 构造一个空栈S,栈顶指针置空 S = NULL; return OK;}Status Push(LinkStack &S,ElemType e){ LinkStack p; p = new StackNode; // 生成新结点 p->data = e; // 将新结点数据域置为e p->next = S; // 将新结点插入栈顶 S = p; // 修改栈顶指针为p return OK;}// 链栈的出栈Status Pop(LinkStack &S,ElemType &e){ // 删除S的栈顶元素,用e返回其值 LinkStack p; if(S==NULL) return ERROR; // 栈空 e = S->data; // 将栈顶元素赋给e p=S; // 用p临时保存栈顶元素空间,以备释放 S=S->next; // 修改栈顶指针 delete p; // 释放原栈顶元素的空间 return OK;}// 取链栈的栈顶元素ElemType GetTop(LinkStack S){ // 返回S的栈顶元素,不修改栈顶指针 if(S != NULL) // 栈非空 return S->data; // 返回栈顶元素的值,栈顶指针不变 return ERROR;}int main(){ LinkStack S; // 列表菜单 cout<<"1 InitStack"<<endl; cout<<"2 Push"<<endl; cout<<"3 Pop"<<endl; cout<<"4 GetTop"<<endl; int choice,e; while(1) { cout<<"Input a choice: "; cin>>choice; // 选择 switch(choice) { case 1: InitStack(S); continue; case 2: Push(S,3); continue; case 3: Pop(S,e); continue; case 4: cout<<"Get: "<<GetTop(S)<<endl; continue; default: cout<<"End..."<<endl; break; } break; } return 0;}
0 0
- 数据结构4:栈的表示和实现
- 数据结构(4):链栈的表示和实现
- 栈的顺序表示和实现(数据结构)
- 重温数据结构:栈的顺序表示和实现与栈的链式表示和实现
- 数据结构(3):顺序栈的表示和实现
- 【数据结构】串的块链存储表示和实现
- 单链表的表示和实现 - 数据结构
- 数据结构学习 串的表示和实现
- 数据结构 - 串的存储表示和实现
- 队列的链式表示和实现(数据结构)
- 循环队列的表示和实现(数据结构)
- 数据结构(16)顺序栈的表示和算法表示
- 链栈的表示和实现
- 栈的表示和实现
- 栈的表示和实现
- 栈的表示和实现
- 数据结构之:线性表的顺序表示和实现
- 数据结构—线性表的链式表示和实现
- 转载的python简史
- Hrbust Online Judge 病毒
- Python urllib模块的URL编码解码功能
- 感恩的心
- ffmpeg avcodec_decode_audio4解码 swr_convert 转换为AV_SAMPLE_FMT_S16格式
- 数据结构(4):链栈的表示和实现
- 工具类:快速获取控件 frame 值(UIView 分类)
- windows下Tomcat配置多实例
- android studio单元测试
- centos下配置多个SVN版本库方法
- Python中的类
- 172. Factorial Trailing Zeroes
- SDAU 贪心算法 03 霍夫曼树
- Canvas和Paint那点事(2)