万金流
初次使用博客园,目前感觉还不错。 不知不觉用了4年零4个月了,越来越喜欢博客园。

判断空,出入,获取栈顶元素

代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 struct Stack
 4 {
 5     int a[100];
 6     int p;
 7 };
 8 bool isempty(Stack *s)
 9 {
10     return s->p==-1;
11 }
12 void push(Stack *s,int x)
13 {
14     s->p++;
15     s->a[s->p]=x;
16 }
17 int pop(Stack *s)
18 {
19     return s->a[s->p--];
20 }
21 int gettop(Stack *s)
22 {
23     return s->a[s->p];
24 }
25 main()
26 {
27     Stack *mystack=new Stack;
28     mystack->p=-1;
29     int n;
30     cin>>n;
31     for(int i=1,t;i<=n;i++)
32     {
33         cin>>t;
34         push(mystack,t);
35     }
36     cout<<gettop(mystack)<<endl;
37     while(!isempty(mystack))
38     {
39         cout<<pop(mystack)<<" ";
40     }
41 }

end here.

posted on 2019-08-27 17:06  万金流  阅读(240)  评论(0编辑  收藏  举报