顺序栈的输入输出问题。以下是我的代码,请问有什么错误?
发布网友
发布时间:2022-05-01 18:45
我来回答
共1个回答
热心网友
时间:2022-06-21 10:48
以下是修改后的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define L sizeof(struct status)
#define max 100
typedef struct status
{
int data[max];
int top;
}status;
status *initial(status *s)
{
if((s=(status*)malloc(L))==NULL)
{
printf("ERROR!");
}
s->top=-1;
return s;
}
status push(status *s)
{
int x=1;
if(s->top>=max-1)
{
printf("FULL!");
return *s;
}
while(x!=0)
{
scanf("%d",&x);
s->top++;
s->data[s->top]=x;
}
s->top--;
return *s;
}
int pop(status *s)
{
int i,x=0;
if(s->top==-1)
{
printf("EMPTY!");
}
for(i=0;s->top>=0;i++)
{
x=s->data[s->top];
s->top--;
printf("%d ",x);
}
printf("\n");
return x;
}
int main()
{
struct status *s=NULL;
s=initial(s);
printf("The numbers are:");
push(s);
pop(s);
return 0;
}