“共享栈”实际代码实现【物联网1132-11】
2014-10-30 23:25
316 查看
#include<iostream> using namespace std; const int StackSize = 100; template<class T> class BothStack { public: BothStack(){top1=-1;top2=StackSize;} ~BothStack(){} void Push(int i,T x); T Pop(int i); T GetTop(int i); int Empty(int i); private: T data[StackSize]; int top1,top2; }; template<class T> void BothStack<T>::Push(int i,T x) { if(top1==top2-1)throw"错误"; if(i==1)data[++top1]=x; if(i==2)data[--top2]=x; } template<class T> T BothStack<T>::Pop(int i) { if(i==1) { if(top1==-1)throw"错误"; return data[top1--]; } if(i==2) { if(top2==StackSize)throw"错误"; return data[top2++]; } } template<class T> T BothStack<T>::GetTop(int i) { if(i==1) { if(top1==-1)throw"错误"; cout<<"这个元素是:"<<data[top1]<<endl; } if(i==2) { if(top2==StackSize)throw"错误"; cout<<"这个元素是:"<<data[top2]<<endl; } return 0; } template<class T> int BothStack<T>::Empty(int i) { if(i==1) { if(top1==-1) cout<<"栈1空"<<endl; else cout<<"栈1 is not 空"<<endl; } if(i==2) { if(top2==StackSize) cout<<"栈2空"<<endl; else cout<<"栈2 is not 空"<<endl; } return 0; } int main() { BothStack<int>s1; int m; do{ cout<<"-------------------------"<<endl; cout<<"输入1开始入栈"<<endl; cout<<"输入2开始弹栈"<<endl; cout<<"输入3开始读取栈顶"<<endl; cout<<"输入4开始判断是否空"<<endl; cin>>m; switch(m) { case 1: int a; double b; cout<<"请输入哪个栈和入栈的元素值"<<endl; cin>>a>>b; s1.Push(a,b); break; case 2: int c; cout<<"请输入要取哪个栈的栈顶(出栈)"<<endl; cin>>c; s1.Pop(c); break; case 3: int v; cout<<"请输入要取哪个栈的栈顶(读取值而已)"<<endl; cin>>v; s1.GetTop(v); break; case 4: int h; cout<<"请输入想判断“哪个栈”为空"<<endl; cin>>h; s1.Empty(h); break; } }while(1); return 0; }
相关文章推荐
- “循环双链表”实际代码宣布实现【物联网1132-11】
- “链栈”实际代码实现【物联网1132-11】
- 第一章 绪论学后心得【物联网1132-11】
- 11、手机防盗--向导代码实现
- 实现一个函数,传递一个有10个已经排好序的整形元素数组(数组大小实际为11)中,将一个指定的值按顺序插入,并在main函数中将插入后的数组输出。
- C++数据结构课程设计报告(1号题)【物联网1132-11】
- 在实际项目中实现把swf资源里面的MC进行转化成按钮,实现代码的复用
- [作业10-11]1.编写一个类立方体Cub,让其实现IEnumarable接口,细节是令其可以遍历迭代长宽高,并做一个客户代码验证。
- 什么是共享栈,以及共享栈的代码实现
- 实验二:线性表的实验【物联网1132-11】
- js获取对象、数组的实际长度,元素实际个数的实现代码
- oc内存管理中retain属性对应的setter实际语法代码实现及原理
- 第一章思维导图【物联网1132-11】
- 快速开发11之多次点击事件实现功能的万能代码
- 第三章 思维导图【物联网1132-11】
- 第二章线性表设计2【物联网1132-11】
- java在线支付---09,10,11,12_在线支付_分析易宝支付网关的应答协议与处理代码,完成用于处理支付响应的Servlet的初步编写和调试,完成处理支付网关响应结果的Servlet,支付实现
- 练习写C++代码(11)--实现简单的时钟类3
- 实验一VC++编程环境的灵活应用 【物联网1132-11】
- 第三章 学后心得及总结 【物联网1132-11】