《数据结构》实验【两栈】
发布时间:2020-12-15 06:05:09 所属栏目:安全 来源:网络整理
导读:#include iostreamusing namespace std;const int Max=100;template class Tclass BothStack{public:BothStack(){top1=-1;top2=Max;}~BothStack(){}void Push(int i,T x);void Pop(int i);private:T data[Max];int top1,top2;};template class Tvoid BothSta
#include <iostream> using namespace std; const int Max=100; template <class T> class BothStack { public: BothStack(){top1=-1;top2=Max;} ~BothStack(){} void Push(int i,T x); void Pop(int i); private: T data[Max]; 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; cout<<data[top1]<<endl;} if(i==2){data[--top2]=x; cout<<data[top2]<<endl;} } template <class T> void BothStack<T>::Pop(int i) { if(i==1) { if(top1==-1)throw"下溢"; cout<<data[top1--]<<endl; } if(i==2) { if(top2==Max)throw"下溢"; cout<<data[top2++]<<endl; } } int main() { BothStack<int> one; cout<<"-----------top1入栈-------------"<<endl; one.Push(1,80); one.Push(1,85); cout<<"-----------top2入栈-------------"<<endl; one.Push(2,90); one.Push(2,95); cout<<"-----------top1出栈-------------"<<endl; one.Pop(1); one.Pop(1); cout<<"-----------top2出栈-------------"<<endl; one.Pop(2); one.Pop(2); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |