《数据结构》实验【顺序栈】

前端之家收集整理的这篇文章主要介绍了《数据结构》实验【顺序栈】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #include <iostream>
  2. using namespace std;
  3. const int Max=100;
  4.  
  5. template <class T>
  6. class SeqStack
  7. {
  8. public:
  9. SeqStack(){top=-1;}
  10. ~SeqStack(){}
  11. void Push(T data);
  12. void Pop();
  13. private:
  14. T data[Max];
  15. int top;
  16.  
  17. };
  18.  
  19.  
  20. template <class T>
  21. void SeqStack<T>::Push(T x)
  22. {
  23. if(top==Max-1)throw"上溢";
  24. data[++top]=x;
  25. cout<<x<<endl;
  26. }
  27.  
  28.  
  29. template <class T>
  30. void SeqStack<T>::Pop()
  31. {
  32. int x;
  33. if(top==-1)throw"下溢";
  34. x=data[top--];
  35. cout<<x<<endl;
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. int main()
  45. {
  46. SeqStack<int> one;
  47. one.Push(123);
  48. one.Push(124);
  49. one.Pop();
  50. one.Pop();
  51. return 0;
  52. }

猜你在找的数据结构相关文章