C语言版数据结构——栈的初始化问题

GPU小码农 GPU小码农 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
不知为什么,程序一运行就异常停止。求大神帮看看哪里不对啊!!
#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++;
}
}
...全文
评论 13
收藏 已收藏
分享
返回顶部
GPU小码农
C语言 70,039 成员 243,237 内容