70,023
社区成员




#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++;
}
}
SqStack S; // SqStack *S;
[quote=引用 7 楼 buyong 的回复:] [quote=引用 6 楼 huiguimoyu 的回复:] [quote=引用 5 楼 AnYidan 的回复:] [quote=引用 4 楼 huiguimoyu 的回复:] [quote=引用 1 楼 jerry_dqh 的回复:] 在发生问题后,看一下在哪儿断住的
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 Push(SqStack *S, SElemType e)//压栈操作
{
#define USE_REALLOC 1
if(S->top - S->base >= S->stacksize)
{
//申请一块更大的内存
#if USE_REALLOC
// realloc 重新申请更大内存。会自动搬迁数据。
SElemType * p =(SElemType *)realloc(S->base,(S->stacksize + STACKCREMENT) * sizeof(SElemType));
if( !p ){ free(S->base); exit(OVERFLOW);}
S->base = p;
#else //if USE_MALLOC
//malloc 版重新申请更大内存,自己搬迁数据。
SElemType * p = (SElemType *)malloc((S->stacksize + STACKCREMENT) * sizeof(SElemType));
if(!S->base){
free(S->base);
exit(OVERFLOW);
}
memcpy(p,S->base,S->stacksize * sizeof(SElemType));
free(S->base);
S->base = p;
#endif
S->top = S->base + S->stacksize;//
S->stacksize += STACKCREMENT;
}
printf("xx");//debug
*(S->top) = e;
printf("%d",*(S->top));
S->top++;
return OK;
#undef USE_REALLOC
}
void PrintElem(const SElemType *e)
{
printf("%d ",*e);
}
void Print(SqStack *S)//打印
{
SElemType *p;
p=S->base;
if(S->base == S->top)
{
printf("\n空栈!没有数据用来输出!\n");
exit(FLASE);
}
else
{ //printf("%d"); 开玩笑,这能输出啥子??缺少参数 %d,格式需要一个整形参数。
// p++;
while(p!= S->top)
PrintElem(p++); //输出栈里存放的所有数。
}
}
添加一个函数,释放内存。
Destroy(SqStack *S){
free(S->base);
}
//修改main,释放内存。
int main()
{
int N = 4;
SqStack S;
InitStack(&S);//初始化一个栈
Push(&S,5); //放一个数字5进去
Print(&S); //看看能不能输出来
Destroy(&S); //释放内存
return 0;
}
[quote=引用 6 楼 huiguimoyu 的回复:] [quote=引用 5 楼 AnYidan 的回复:] [quote=引用 4 楼 huiguimoyu 的回复:] [quote=引用 1 楼 jerry_dqh 的回复:] 在发生问题后,看一下在哪儿断住的
[quote=引用 5 楼 AnYidan 的回复:] [quote=引用 4 楼 huiguimoyu 的回复:] [quote=引用 1 楼 jerry_dqh 的回复:] 在发生问题后,看一下在哪儿断住的
[quote=引用 4 楼 huiguimoyu 的回复:] [quote=引用 1 楼 jerry_dqh 的回复:] 在发生问题后,看一下在哪儿断住的
[quote=引用 1 楼 jerry_dqh 的回复:] 在发生问题后,看一下在哪儿断住的
在发生问题后,看一下在哪儿断住的
明显的S野指针,没初始化就用上了