您的位置:首页 > 其它

使用数组实现堆栈

2011-05-22 14:46 316 查看
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

int stack[10];
int top=-1;
void push(int number)
{
printf("push a number\n");
if(top>=9)
{
printf("the stack is full");
}else{
top++;
stack[top]=number;
}
}

int  pop()
{
int temp;
printf("pop a number\n");
if(top<0)
{
printf("the stack is empty");
return -1;
}else{
temp=stack[top];
top--;
printf("%d",temp);
return temp;
}
}

void show()
{

for(;top>=0;top--)
{
printf("%d\t",stack[top]);
}
}

int main()
{
int select;
int number;

printf("(1)please input a stack data\n");
printf("(2)please output a stack data\n");
printf("(3)exit\n");
printf("please input one by input a number");

scanf("%d",&select);

while(select!=3)
{
switch(select)
{
case 1:scanf("%d",&number);push(number);break;
case 2:pop();break;
default:exit;break;
}
printf("(1)please input a stack data\n");
printf("(2)please output a stack data\n");
printf("(3)exit");
printf("please input one by input a number\n");

scanf("%d",&select);
}

show();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: