用c的数组简单的模拟了入栈

其实很简单,只要控制住输出时倒输出、且只输出一个

#include <stdio.h>
#include <stdlib.h>
int zhan[20];
int n=-1;
void ru();
void chu();
int main()
{
int a=0;
while(1)
{
printf("按1入栈,按二出栈,退出按0 ");
scanf("%d",&a);
switch(a)
{
case 1:ru();break;
case 2:chu();break;
default:printf("输入错误即将退出 ");return 0;
}
}
}
void ru()
{
printf("请输入入栈的元素 ");
n=n+1;
scanf("%d",&zhan[n]);
}
void chu()
{
int i=n;
printf("出栈的元素: ");
for(;i>=0;i--)
{
printf("%d ",zhan[i]);
n=n-1;
break;
}
}

原文地址:https://www.cnblogs.com/maodun/p/6165733.html