第三章顺序栈

来源:互联网 发布:vb中的对象 编辑:程序博客网 时间:2024/05/01 08:18
#include<iostream>using namespace std;const int StackSize=10;template<class T>class SeqStack{public:SeqStack();~SeqStack(){}void Push(T x);T Pop();    T GetTop();int Empty();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"shangyi";data[++top]=x;}template<class T>T SeqStack<T>::Pop(){if(top==-1)throw"xiqyi";T x=data[top--];return x;}template<class T>T SeqStack<T>::GetTop(){if(top!=-1)return data[top];}template<class T>int SeqStack<T>::Empty(){if(top==-1)return 1;elsereturn 0;}void main(){SeqStack<int>S;if(S.Empty())cout<<"饯空"<<endl;elsecout<<"见非空"<<endl;cout<<"对15和10执行入践操作"<<endl;S.Push(15);S.Push(10);cout<<"践顶元素为:"<<endl;cout<<S.GetTop()<<endl;cout<<"执行一次出践操作"<<endl;S.Pop();cout<<"践顶元素:"<<endl;cout<<S.GetTop()<<endl;}

0 0
原创粉丝点击