我试图执行堆栈操作,并在执行push操作时输入的值始终为0. 如果输入任何数字,则加载的数组中的结果始终为0.堆栈错误按下操作对阵列进行操作
//Stack Operation
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int stack[10];
int top=0;
void push()
{
int i;
printf("Enter the element you want to add");
scanf("%d",& stack[top]);
top++;
printf("%d",stack[top]);
printf("The element is added\n");
for(i=0;i<top;i++) {
printf("%d",stack[top]);
}
}
int pop()
{
top--;
return(stack[top]);
}
void display()
{
int i;
for(i=0;i<=top;i++);
{
printf("%d \t",stack[i]);
}
}
void main()
{
int ch;
clrscr();
label:
printf("1---->Push\n");
printf("2---->Pop\n");
printf("3----->Display\n");
printf("4-----> Exit\n");
printf("Enter your choice");
scanf("%d",&ch);
if(ch==1) {
clrscr();
push();
goto label;
}
if(ch==2) {
int f;
clrscr();
f=pop();
printf("Poped Element %d",f);
goto label;
}
if(ch==3) {
clrscr();
display();
goto label;
}
if(ch==4) {
exit(0);
}
getch();
}
你确定你想在'scanf(“%d”,&stack [top])之间增加一个值吗? top ++; printf(“%d”,stack [top]);' –
我想我弄错了。 –