ch3-栈和队列-顺序栈的入栈与出栈操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>

//元素elem进栈
int push(int *a,int top,int elem){
a[++top]=elem;
return top;
}

//数据元素出栈
int pop(int *a,int top){
if(top==-1){
printf("空栈");
return -1;
}
printf("弹栈元素:%d\n",a[top]);
top--;
return top;
}

int main() {
int a[100];
int top=-1;
top=push(a, top, 1);
top=push(a, top, 2);
top=push(a, top, 3);
top=push(a, top, 4);
top=pop(a, top);
top=pop(a, top);
top=pop(a, top);
top=pop(a, top);
top=pop(a, top);
return 0;
}

运行结果

1
2
3
4
5
弹栈元素:4
弹栈元素:3
弹栈元素:2
弹栈元素:1
空栈

凡希 wechat
喜欢所以热爱,坚持干货分享,欢迎订阅我的微信公众号
呐,请我吃辣条