实验三 链栈(c++)
来源:互联网 发布:软件源代码查看器 编辑:程序博客网 时间:2024/06/05 04:36
一、实验目的
1、 熟练掌栈和队列的结构特点,掌握栈和队列的顺序存储和链式存储结构和实现。
2、 学会使用栈和队列解决实际问题。
二、实验内容
1、 自己确定结点的具体数据类型和问题规模:
建立一个链栈,实现栈的压栈和出栈操作。
#include<iostream>using namespace std;const int StackSize=30;template <class Datatype>class SeqStack{public:SeqStack(){top=-1;}~SeqStack(){}void Push(Datatype x);Datatype Pop();Datatype Get(){if(top!=-1) return data[top];}int Empty(){if(top==-1) return 1;else return 0;}private:Datatype data[StackSize];int top;};
template<class Datatype>void SeqStack<Datatype>::Push(Datatype x){if(top==StackSize-1) throw"上溢";data[++top]=x;}template <class Datatype>Datatype SeqStack<Datatype>::Pop(){Datatype x;if(top==-1)throw"下溢";x=data[top--];return x;}
int main(){ SeqStack<int> S; if(S.Empty()) cout<<"栈为空!"<<endl; else cout<<"栈非空!"<<endl; cout<<"构造一个空栈!"<<endl; cout<<"将5和4进行入栈操作!"<<endl;S.Push(5);S.Push (4);cout<<"栈顶元素为:";cout<<S.Get();cout<<endl;cout<<"进行第一次出栈操作!"<<endl;S.Pop();cout<<"栈顶元素为:";cout<<S.Get();cout<<endl;}
阅读全文
0 0
- 实验三 链栈(c++)
- [C++]实验三
- C语言程序设计实验手册--实验三
- [C]计算机图形学实验三
- 实验三循环队列(c++)
- 实验三 链栈
- 实验三、链栈
- 实验三 顺序栈(c++)
- 实验三 链队列(c++)
- 实验三 端口扫描实验
- 实验三:模块化实验
- 实验三
- 实验三
- 实验三
- 实验三
- 实验三
- 实验三
- 实验三
- Sharpdevelop如何在项目中添加类文件
- PS如何绘制虚线圆
- Windows如何安装MSMQ消息队列
- JS中的“!!”
- VC++ 提示无法打开包括文件“iostream.h”怎么办
- 实验三 链栈(c++)
- AE After Effect 如何替换和修改素材
- AE After Effect 如何分段渲染
- AutoCAD菜单加载失败 找不到文件mnc 怎么办
- AE After Effect 渲染如何输出设置
- AutoCAD如何打印
- AutoCAD如何编辑块,打散块
- AutoCAD如何方便截图放到Word文档,改成白底黑字
- AutoCAD如何倒角 倒圆角 倒直角