前端之家收集整理的这篇文章主要介绍了
《数据结构》实验【顺序栈】,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- #include <iostream>
- using namespace std;
- const int Max=100;
-
- template <class T>
- class SeqStack
- {
- public:
- SeqStack(){top=-1;}
- ~SeqStack(){}
- void Push(T data);
- void Pop();
- private:
- T data[Max];
- int top;
-
- };
-
-
- template <class T>
- void SeqStack<T>::Push(T x)
- {
- if(top==Max-1)throw"上溢";
- data[++top]=x;
- cout<<x<<endl;
- }
-
-
- template <class T>
- void SeqStack<T>::Pop()
- {
- int x;
- if(top==-1)throw"下溢";
- x=data[top--];
- cout<<x<<endl;
- }
-
-
-
-
-
-
-
- int main()
- {
- SeqStack<int> one;
-
- one.Push(123);
- one.Push(124);
- one.Pop();
- one.Pop();
- return 0;
- }