顺序栈
来源:互联网 发布:修复老电影软件下载 编辑:程序博客网 时间:2024/06/13 23:38
#include <iostream >using namespace std;const int StackSize=10;template<class T>class SeqStack{public:SeqStack();~SeqStack(){}void push(T x);int pop();int gettop();int empty();void print();private:T data[StackSize];int top;};template <class T>SeqStack<T>::SeqStack(){ top=-1;}template <class T>void SeqStack<T>::push(T x){if(top==StackSize-1)throw"上溢";data[++top]=x;}template <class T>int SeqStack<T>::pop(){T x;if(top==-1)throw"下溢";x=data[top--];return x;}template <class T>int SeqStack<T>::gettop(){if(top!=-1) return data[top];}template <class T>int SeqStack<T>::empty(){if(top==-1)return 1;else return 0;}template< class T > void SeqStack<T>::print() { for( int i=0 ; i<=top ; i++) { cout<<data[i]<<" ";} cout<<endl; } void main(){ SeqStack<int>s; if(s.empty()) cout<<"栈为空"<<endl; else cout<<"栈非空"<<endl; cout<<"依次将1,2,3入栈"<<endl; s.push(1); s.push(2); s.push(3); s.print(); cout<<"\n"<<endl; cout<<"3元素出栈"<<endl; s.pop(); s.print();}
阅读全文
0 0
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 顺序栈
- 用户登录操作
- Jzoj4736 漆黑列车载运数个谎言(GOSICK系列)
- 基于java的设计模式(3)工厂
- Matlab基于主分量的人脸重建显示
- CentOS 7.3下使用yum安装MySQL
- 顺序栈
- CMN题目描述
- 蚁群算法及其应用
- v-if和v-show区别
- 科普——色彩模式
- 关于一次ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction的解决办法
- JS:throw try/catch/finally语句
- C 语言现代编程一 C与模块化
- 分布式事务之说说TCC事务