【求助】写了一个顺序栈,程序运行中提示终止,搞了好久没找到原因
源代码:程序代码:
#define OK 1 #define ERROR 0 #define MAXSIZE 50 #include <stdlib.h> #include <stdio.h> typedef int SElementType; typedef int Status; typedef struct{ SElementType data[MAXSIZE]; int top; }SqStack; void InitStack(SqStack *S){ S->top = -1; } Status Push(SqStack *S, SElementType e){ if(S->top == MAXSIZE - 1){ return ERROR; } S->data[++S->top] = e; return OK; } Status Pop(SqStack *S,SElementType *e){ if(S->top == -1){ return ERROR; } *e = S->data[S->top]; S->top--; return OK; } int StackLength(SqStack *S){ int len = 0; len = S->top+1; return len; } int main(){ SqStack *S; Status sts; int i = 0; SElementType *stackElement; S = (SqStack *)malloc(sizeof(SqStack)); InitStack(S); for(i = 0; i < 10; i++){ sts = Push(S,i); printf("push %d,%s\n",i,(sts == OK) ? "success" : "failure"); } printf("stack length:%d\n",StackLength(S)); for(i = 0; i < 1; i++){ sts = Pop(S,stackElement); printf("pop:%d,sts:%s\n",*stackElement,(sts == OK) ? "success" : "failure"); } printf("stack length:%d\n",StackLength(S)); return 0; }
程序运行中提示错误:
本人C菜鸟,不知道哪里出问题了,搞了好久,快崩溃了,先谢谢了
[此贴子已经被作者于2016-3-22 13:12编辑过]