1 //数组实现 栈
2 #include <iostream>
3 using namespace std;
4
5 struct Sqstack{
6 int data[100];
7 int * base;
8 int * top;
9 int len ; //当前已经用的长度
10 };
11 void InitStack(Sqstack &s){
12 s.base=s.data;
13 s.top=s.base;
14 s.len=0;
15 }
16
17 void Push(Sqstack &s,int e) //入栈
18 {
19 if (s.len<100)
20 {
21 *s.top=e;
22 s.top++;
23 s.len++;
24 }
25 return 0;
26 }
27
28 int Pop(Sqstack &s) ///出栈, 注意直接引用,
29 {
30 if (s.base==s.top)
31 return -1;
32 int e=*(s.top-1);
33 s.top--;
34 s.len--;
35 return e;
36 }
37
38 void main(){
39
40 Sqstack s;
41 InitStack(s);
42 cout<<"请输入少于100个数(ctrl +d 结束输入):"<<endl;
43 int temp;
44
45 while (cin>>temp) //ctrl +d 结束
46 {Push(s,temp);
47 }
48
49 //出栈
50 cout<<Pop(s)<<endl;//出栈1
51 cout<<Pop(s)<<endl;//出栈2
52 cout<<Pop(s)<<endl;//出栈3
53 }