#include<stdio.h>
#include<stdlib.h>
int stack[10]; //stack堆
int top = 0; //栈顶指针top,指向实际栈顶后的空位置,初值为0
void push(int x);
int pop(void);
int main()
{
int i;
for(i=0; i<10; i++) //入栈
push(i);
for(i=9; i>=0; i--) //倒着输出
printf("%d\n",stack[i]);
printf("pop first 5 elements\n");
for(i=0; i<5; i++)
{
printf("The %d element is %d\n", i+1, pop());
}
return 0;
}
void push(int x) //入栈算法
{
if(top == 10) //top = 10,栈满
{
printf("overflow\n"); //上溢
exit(2);
}
stack[top] = x;
top++;
}
int pop(void)
{
if(top == 0)
{
printf("stack empty\n"); //下溢
exit(3);
}
top--;
return stack[top];
}
程序运行结果
如果感觉不错,那就给我点个赞吧(ノ ̄▽ ̄)