1 #include <iostream>
 2 #include <stdlib.h>
 3 using namespace std;
 4 
 5 #define maxSize 30
 6 
 7 typedef struct
 8 {
 9     int data[maxSize];
10     int top;
11 }SqStack;
12 
13 void InitStack(SqStack &S)
14 {
15     S.top=-1;
16 }
17 
18 int IsEmpty(SqStack S)
19 {
20     if(S.top==-1)
21         return 1;
22     else
23         return 0;
24 }
25 
26 int Push(SqStack &S,int e)
27 {
28     if(S.top==maxSize-1)
29         return 0;
30     ++(S.top);
31     S.data[S.top]=e;
32     return 1;
33 }
34 
35 int Pop(SqStack &S,int &e)
36 {
37     if(S.top==-1)
38         return 0;
39     e=S.data[S.top];
40     --(S.top);
41     return 0;
42 }
43 
44 int GetTop(SqStack S,int &e)
45 {
46     if(S.top==-1)
47         return 0;
48     e=S.data[S.top];
49     --(S.top);
50     return 1;
51 }
52 
53 void Print(SqStack S)
54 {
55     if(S.top==-1)
56         cout<<"顺序栈为空!"<<endl;
57     else
58     {
59         while(S.top!=-1)
60         {
61             cout<<"  "<<S.data[S.top--]<<"  ";
62         }
63     }
64 }
65 
66 int main()
67 {
68     int i=0;
69     int e;
70     SqStack S;
71     InitStack(S);
72 
73     cout<<"\n--------------------------------\n";
74     cout<<"\n顺序栈中的元素为:\n";
75     for(i=0;i<10;++i)
76         Push(S,i);
77     Print(S);
78     if((e=IsEmpty(S))==1)
79         cout<<"\n顺序栈为空!\n";
80     else
81         cout<<"\n顺序栈非空!\n";
82 
83     cout<<"\n--------------------------------\n";
84     cout<<"\n删除栈顶元素后顺序栈中的元素为:\n";
85     Pop(S,e);
86     Print(S);
87 
88     cout<<"\n--------------------------------\n";
89     GetTop(S,e);
90     cout<<"\n此时栈顶元素为:"<<e<<endl;
91     cout<<"\n顺序栈中的元素为:\n";
92     Print(S);
93     return 0;
94 }

 

posted on 2015-12-30 08:53  Xbert  阅读(137)  评论(0编辑  收藏  举报