C语言版数据结构——栈的初始化问题
不知为什么,程序一运行就异常停止。求大神帮看看哪里不对啊!!
#define FLASE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKCREMENT 10
typedef int SElemType;
typedef int Status;
typedef struct
{
SElemType * base;
SElemType * top;
int stacksize;
}SqStack;
//////////////////////////////////////////////
Status InitStack(SqStack *S);
Status Push(SqStack *S, SElemType e);
Status Pop(SqStack *S, SElemType *e);
void Print(SqStack *S);
//////////////////////////////////////////////
void main()
{
int N = 4;
SqStack *S;
InitStack(S);//初始化一个栈
Push(S,5); //放一个数字5进去
Print(S); //看看能不能输出来
}
Status InitStack(SqStack *S) //初始化一个空栈
{
S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S->base)exit(OVERFLOW);
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack *S, SElemType e)//压栈操作
{
if(S->top - S->base >= S->stacksize)
{
S->base = (SElemType *)malloc((S->stacksize + STACKCREMENT) * sizeof(SElemType));
if(!S->base)exit(OVERFLOW);
S->top = S->base + S->stacksize;//
S->stacksize += STACKCREMENT;
}
printf("xx");//debug
*(S->top) = e;
printf("%d",*(S->top));
S->top++;
return OK;
}
Status Pop(SqStack *S, SElemType *e)//出栈
{
if(S->base == S->top)return ERROR;
*e = *(S->top);
--S->top;
return OK;
}
void Print(SqStack *S)//打印
{
int *p;
p=S->base;
if(S->base == S->top)
{
printf("\n空栈!没有数据用来输出!\n");
exit(FLASE);
}
else
{
printf("%d");
p++;
}
}
...全文


