【1】栈的基本概念

(1)栈是限定仅在表尾进行插入和删除操作的线性表。所谓的表尾是指栈顶,而不是栈底。

(2)栈是后进先出的线性表。

(3)把允许插入和删除的一端称为栈顶,另一端称为栈底。

(4)不含任何元素的栈称为空栈。 判定条件为top等于-1。

(5)栈是一个线性表,栈元素具有线性关系。

【2】进栈出栈变化形式

进栈出栈变化形式如下图:

【3】栈的实现代码

顺序栈的实现:

  1 // SeqStack.h
  2 // 顺序栈的实现
  3 #pragma  once
  4 
  5 #include <assert.h>
  6 #include <string.h>
  7 
  8 #define   STACKSIZE  100
  9 
 10 template<class Type>
 11 class   SeqStack
 12 {
 13     private:
 14         Type *data;
 15         int  top;
 16         int  size;
 17     public:
 18         SeqStack(int  sz = STACKSIZE);
 19         ~SeqStack();
 20         SeqStack(const SeqStack<Type> &st);
 21         SeqStack<Type> operator=(const SeqStack<Type> &st);
 22         bool Push(const Type &item);
 23         bool Pop(Type &item);
 24         bool GetTop(Type &item);
 25         bool IsEmpty() const;
 26         bool IsFull() const;
 27         void MakeEmpty();
 28         int  StackLen();
 29         void PrintStack();
 30 };
 31 
 32 template<class Type>
 33 SeqStack<Type>::SeqStack(int sz)
 34 {
 35     size = sz > STACKSIZE ? sz : STACKSIZE;
 36     data = new Type[size];
 37     assert(data!=NULL);
 38     for (int i = 0; i < size; i++)
 39     {
 40         data[i] = NULL;
 41     }
 42     top = -1;
 43 }
 44 template<class Type>
 45 SeqStack<Type>::~SeqStack()
 46 {
 47     if (data != NULL)
 48     {
 49         delete []data;
 50         data = NULL;
 51     }
 52     size = 0;
 53     top = -1;
 54 }
 55 template<class Type>
 56 SeqStack<Type>::SeqStack(const SeqStack<Type> &st)
 57 {
 58     size = st.size;
 59     data = new Type[size];
 60     assert(data != NULL);
 61     memcpy(data, st.data, (st.top + 1)*sizeof(Type));
 62     top = st.top;
 63 }
 64 template<class Type>
 65 SeqStack<Type>  SeqStack<Type>::operator=(const SeqStack<Type> &st)
 66 {
 67     if (this != &st)
 68     {
 69         delete []data;
 70         size = st.size;
 71         data = new Type[size];
 72         assert(data != NULL);
 73         memcpy(data, st.data, (st.top + 1)*sizeof(Type));
 74         top = st.top;
 75     }
 76     return *this;
 77 }
 78 template<class Type>
 79 bool SeqStack<Type>::Push(const Type &item)
 80 {
 81     if (top < size-1)
 82     {
 83         data[++top] = item;
 84         return true;
 85     }
 86     else
 87     {
 88         return false;
 89     }
 90 }
 91 template<class Type>
 92 bool SeqStack<Type>::Pop(Type &item)
 93 {
 94     if (top >= 0)
 95     {
 96         item = data[top--];
 97         return true;
 98     }
 99     else 
100     {
101         return false;
102     }
103 }
104 template<class Type>
105 bool SeqStack<Type>::GetTop(Type &item)
106 {
107     if (top >= 0)
108     {
109         item = data[top];
110         return true;
111     }
112     return false;
113 }
114 template<class Type>
115 bool SeqStack<Type>::IsEmpty() const
116 {
117     return -1 == top;
118 }
119 template<class Type>
120 bool SeqStack<Type>::IsFull() const
121 {
122     return top >= size - 1;
123 }
124 template<class Type>
125 void SeqStack<Type>::MakeEmpty()
126 {
127     top = -1;
128 }
129 template<class Type>
130 int SeqStack<Type>::StackLen()
131 {
132     return  top + 1;
133 }
134 template<class Type>
135 void SeqStack<Type>::PrintStack()
136 {
137     for (int i = top; i >= 0; i--)
138     {
139         cout << data[i] << "  ";
140     }
141     cout << endl;
142 }
View Code


Good  Good  Study, Day  Day  Up.

顺序  选择  循环  总结

posted @ 2013-12-01 16:06  kaizenly  阅读(1428)  评论(0编辑  收藏  举报
打赏