/*用数组对堆栈的操作*/
#include
#define maxsize 10
int stack[maxsize];
int top=0;
void push(int data)
{
int i;
if(top>=maxsize)
printf("\nThe stack is full!\n");
else
{
stack[top]=data;
printf("\nThe stack data is:");
for(i=top;i>=0;i--)
printf("[%d]",stack[i]);
top++;
printf("\n");
}
}
int take()
{
int temp;
int i;
if(top<=0)
printf("\nThe stack is empty!\n");
else
{
top--;
temp=stack[top];
printf("\nThe pop data is [%d]\n",temp);
printf("\nThe stack data is");
for(i=top;i>=0;i--)
printf("[%d]",stack[i]);
printf("\n");
}
}
void main()
{
int select;
int stack[5];
int i,data;
do
{
printf("\n(1) Input a stack data");
printf("\n(2) Output a stack data");
printf("\n(3) Exit");
printf("\nPlease select one:");
scanf("%d",&select);
switch(select)
{
case 1:printf("\nPlease input the data:");
scanf("%d",&data);
push(data);
break;
case 2:take();
break;
case 3:break;
}
}
while(select<3);
getch();
}