c++静态数组实现栈



c++静态数组实现栈。

VS2005运行通过,如有问题,请各位大牛指正。

[cpp] view plaincopy


  1. /*静态栈的条件
  2. 栈顶初始值:top=-1;
  3. 栈顶:总是指向刚刚压入的值
  4. 栈空:top=-1
  5. 栈满:top=Max-1
  6. 入栈: data[++top] = NewItem;
  7. 出栈:newItem = data[top--];
  8. */
  9. #include <iostream>
  10. using namespace std;
  11. const int Max = 50;
  12. template<class Type>
  13. class Stack
  14. {
  15. private:
  16.     Type data[Max];
  17.     int top;
  18. public:
  19.     Stack();
  20.     Stack(const Stack<Type>& otherStack);
  21.     ~Stack();
  22.     Stack<Type> operator=(const Stack<Type>& otherStack);
  23.     void initStack();
  24.     bool isEmptyStack() const;
  25.     bool IsFullStack() const;;
  26.     void destoryStack();
  27.     void pop(Type& data);
  28.     void push(Type data);
  29. };
  30. template<class Type>
  31. Stack<Type>::Stack()
  32. {
  33.     top=-1;
  34. }
  35. template<class Type>
  36. Stack<Type>::Stack(const Stack<Type>& otherStack)
  37. {
  38.     top = otherStack.top;
  39.     if (!otherStack.isEmptyStack())
  40.     {
  41.         for (int i=0;i<=top;i++)
  42.         {
  43.             data[i] = otherStack.data[i];
  44.         }
  45.     }
  46. }
  47. template<class Type>
  48. Stack<Type>::~Stack()
  49. {
  50. }
  51. template<class Type>
  52. Stack<Type> Stack<Type>::operator=(const Stack<Type>& otherStack)
  53. {
  54.     if (this != &otherStack)
  55.     {
  56.         if (!otherStack.isEmptyStack())
  57.         {
  58.             top = otherStack.top;
  59.             for (int i=0;i<=top;i++)
  60.             {
  61.                 data[i] = otherStack.data[i];
  62.             }
  63.         }
  64.     }
  65.     return *this;
  66. }
  67. template<class Type>
  68. void Stack<Type>::initStack()
  69. {
  70.     top=-1;
  71. }
  72. template<class Type>
  73. bool Stack<Type>::isEmptyStack() const
  74. {
  75.     return (top==-1);
  76. }
  77. template<class Type>
  78. bool Stack<Type>::IsFullStack() const
  79. {
  80.     return (top==Max-1);//top表示指针位置,最大为Max-1
  81. }
  82. template<class Type>
  83. void Stack<Type>::destoryStack()
  84. {
  85.     top=-1;
  86. }
  87. template<class Type>
  88. void Stack<Type>::pop(Type& NewItem)
  89. {
  90.     if (isEmptyStack())
  91.     {
  92.         cout<<”栈空”<<endl;
  93.     }
  94.     else
  95.     {
  96.         NewItem = data[top--];
  97.     }
  98. }
  99. template<class Type>
  100. void Stack<Type>::push(Type NewItem)
  101. {
  102.     if (IsFullStack())
  103.     {
  104.         cout<<”栈满!”<<endl;
  105.     }
  106.     else
  107.     {
  108.         data[++top]= NewItem;
  109.     }
  110. }
  111. int main()
  112. {
  113.     Stack<int> s;
  114.     Stack<int> s1;
  115.     s1=s;
  116.     int a[4]={1,2,3,4};
  117.     for (int i=0;i<4;i++)
  118.     {
  119.         s1.push(a[i]);
  120.     }
  121.     for (int i=0;i<4;i++)
  122.     {
  123.         int temp;
  124.         s1.pop(temp);
  125.         cout<<temp<<endl;
  126.     }
  127.     system(“pause”);
  128.     return 0;
  129. }