堆栈的实现

编的一个非常简单的堆栈实现:

 1 #include<iostream>
2 using namespace std;
3 const int MaxSize=100;
4 class stack{
5 private:
6 int sta[MaxSize];
7 int top;
8 public:
9 stack();
10 bool IsEmpty();
11 bool IsFull();
12 void Push(int data);
13 int Pop(void);
14 void dis(void);
15 };
16 ////
17 stack::stack()
18 {
19 top=0;
20 cout<<"the size of stack is:"<<MaxSize<<endl;
21 }
22 ////
23 bool stack::IsEmpty()
24 {
25 return ((MaxSize-1)!=top)?false:true;
26 }
27 ////
28 bool stack::IsFull()
29 {
30 return ((MaxSize-1)==top)?true:false;
31 }
32 ////
33 void stack::Push(int data)
34 {
35 sta[top]=data;
36 top++;
37 }
38 ////
39 int stack::Pop(void)
40 {
41
42 return sta[--top];
43 }
44 ////
45 void stack::dis(void)
46 {
47 for(int i=0;i<top;i++)
48 {
49 cout<<sta[i];
50 cout<<endl;
51 }
52
53 }
54 ////
55 int main()
56 {
57 stack st;
58 st.Push(1);
59 st.Push(2);
60 st.Push(3);
61 st.dis();
62 cout<<st.Pop()<<endl;
63 cout<<st.Pop()<<endl;
64 cout<<st.Pop()<<endl;
65
66 return 0;
67 }



posted on 2012-03-05 09:19  专吃兔子的大黑猫  阅读(218)  评论(0编辑  收藏  举报