栈的实现

该程序定义了链式栈(栈的链式存储结构)的存储结构,并实现了链式栈的基本操作,例如:构造函数、拷贝构造函数和赋值运算符重载函数的实现、析构函数、判空、插入、删除、获取栈顶元素,遍历。

 
  1 #include<iostream>
  2 
  3 using namespace std;
  4 
  5 typedef int StackElement;
  6 
  7 struct Node
  8 
  9 {
 10 
 11      Node *next;
 12 
 13      StackElement data;
 14 
 15      Node(StackElement value, Node *link = NULL) :data(value),next(link)
 16 
 17     {}
 18 
 19 };
 20 
 21 class Stack
 22 
 23 {
 24 
 25 public:
 26 
 27         Stack() :myTop(NULL)
 28 
 29                      {}
 30 
 31          Stack(const Stack& original)
 32 
 33          {
 34 
 35              myTop = NULL;
 36 
 37              if (!original.empty())
 38 
 39             {
 40 
 41                       myTop = new Node(original.Top());  //复制第一个节点
 42 
 43                      Nodeptr lastPtr = myTop;    //设置指针以遍历整个栈的链表
 44 
 45                      Nodeptr origPtr =original.myTop->next;
 46 
 47              while (origPtr != 0)
 48 
 49              {
 50 
 51                      lastPtr->next = new Node(origPtr->data);
 52 
 53                      lastPtr = lastPtr->next;
 54 
 55                      origPtr = origPtr->next;
 56 
 57               }
 58 
 59          }
 60 
 61     }
 62 
 63     ~Stack()
 64 
 65     {
 66 
 67                    Nodeptr ptr = myTop, nextPtr;//ptr将被释放的节点,nextptr是ptr后继
 68 
 69                   while (ptr!=0)
 70 
 71                    {
 72 
 73                             nextPtr = ptr->next;
 74 
 75                             delete ptr;
 76 
 77                             ptr = nextPtr;
 78 
 79                    }
 80 
 81     }
 82 
 83     Stack& operator=(const Stack&rhs)
 84 
 85     {
 86 
 87             if (this!=&rhs)
 88 
 89             {
 90 
 91                    this->~Stack();//销毁当前的链表
 92 
 93                  if (rhs.empty())  //空栈
 94 
 95                       myTop = NULL;
 96 
 97                   else
 98 
 99                   {
100 
101                             myTop = new Node(rhs.Top());   //复制第一个节点
102 
103                           Nodeptr lastPtr = myTop;    //设置指针以遍历整个栈的链表
104 
105                            Nodeptr rhsPtr = rhs.myTop->next;
106 
107                       while (rhsPtr != 0)
108 
109                        {
110 
111                            lastPtr->next = new Node(rhsPtr->data);
112 
113                             lastPtr = lastPtr->next;
114 
115                             rhsPtr = rhsPtr->next;
116 
117                        }
118 
119                  }
120 
121             }
122 
123     return *this;
124 
125     }
126 
127     StackElement Top() const
128 
129     {
130 
131            if (!empty())
132 
133           {
134 
135                  return (myTop->data);
136 
137           }
138 
139          else
140 
141           {
142 
143                 cout << "The Stack is empty!" << endl;
144 
145           }
146 
147     }
148 
149     bool empty() const
150 
151     {
152 
153            return myTop==0;
154 
155     }
156 
157     void push(const StackElement& value)
158 
159     {
160 
161           myTop = new Node(value, myTop);
162 
163     }
164 
165     void Display()
166 
167     {
168 
169          Nodeptr ptr;
170 
171          for (ptr = myTop; ptr != 0; ptr = ptr->next)
172 
173          cout << ptr->data <<" ";
174 
175          cout << endl;
176 
177     }
178 
179     void pop()
180 
181     {
182 
183          if (!empty())
184 
185          {
186 
187                Nodeptr ptr=myTop;
188 
189                myTop = myTop->next;
190 
191                delete ptr;
192 
193          }
194 
195         else
196 
197         {
198 
199                cout << "Stack is empty" << endl;
200 
201         }
202 
203     }
204 
205 private:
206 
207    typedef Node* Nodeptr;
208 
209    Nodeptr myTop;
210 
211 };
212 
213 void main()
214 
215 {
216 
217    Stack s;
218 
219    cout<<s.empty()<<endl;
220 
221    for (StackElement i = 1; i < 6; ++i)
222 
223    {
224 
225            s.push(i);
226 
227    }
228 
229    cout << s.empty() << endl;
230 
231    cout << s.Top() << endl;
232 
233    s.Display();
234 
235    s.pop();
236 
237    s.Display();
238 
239    Stack s1(s);
240 
241    s1.Display();
242 
243    Stack s2;
244 
245     s2 = s;
246 
247     s2.Display();
248 
249     system("pause");
250 
251 }

 

 

 
 

 

posted @ 2016-04-05 11:20  *尘封的记忆*  阅读(204)  评论(0编辑  收藏  举报