- #include <iostream>
- using namespace std;
- const int StackSize=50;
- template<class DataType>
- class SeqStack
- {
- private:
- DataType data[StackSize];
- int top;
- public:
- SeqStack()
- {
- top=-1;
- }
- ~SeqStack()
- {
- cout<<"\\ndestructing... ..."<<endl;
- }
- void Push(DataType x);
- DataType Pop();
- DataType GetTop()
- {
- if(top!=-1)
- return data[top];
- }
- int Empty()
- {
- return top==-1?1:0;
- }
- void Print();
- };
- template<class DataType>
- void SeqStack<DataType>::Push(DataType x)
- {
- if(top==StackSize-1)
- throw "上溢";
- data[++top]=x;
- }
- template<class DataType>
- DataType SeqStack<DataType>::Pop()
- {
- int x;
- if(top==-1)
- throw "下溢";
- x=data[top--];
- return x;
- }
- template<class DataType>
- void SeqStack<DataType>::Print()
- {
- for(int i=0; i<top+1; i++)
- cout<<data[i]<<" ";
- }
- int main()
- {
- SeqStack<int> s;
- for(int i=0; i<20; i++)
- s.Push(i);
- cout<<"Push:";
- s.Print();
- s.Pop();
- cout<<"\\nPop:";
- s.Print();
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2101201511571.html
来源: http://www.codesnippet.cn/detail/2101201511571.html