栈的操作,为什么会出错呢?
#include "stdio.h "
#define MAXNUM 4
typedef struct
{
int data[MAXNUM];
int top;
}seqstack;
void creatstack(seqstack *s)
{
s-> top=-1;
}
int push(seqstack *s,int x)
{
if(s-> top> =MAXNUM-1)
{
printf( "Overflow!\n ");
return 0;
}
else
{
s-> top=s-> top+1;
s-> data[s-> top]=x;
return 1;
}
}
int pop(seqstack *s)
{
int x;
if(s-> top <=-1)
{
printf( "Underflow!\n ");
x=NULL;
}
else
{
x=s-> data[s-> top];
s-> top=s-> top-1;}
return x;
}
void main()
{
seqstack *s;
int a[MAXNUM]={1,2,3,4},i;
creatstack(s);
for (i=0;i <MAXNUM;i++)
{
push(s,a[i]);
}
for (i=0;i <MAXNUM;i++)
{
printf( "%d ",pop(s));
}
}
[解决办法]
seqstack *s = new seqstack;
[解决办法]
seqstack *s = (seqstack *)malloc(sizeof(seqstack));