读书人

栈的操作为什么会出错呢?该怎么处理

发布时间: 2012-04-11 17:42:33 作者: rapoo

栈的操作,为什么会出错呢?
#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));

读书人网 >C语言

热点推荐