早教吧 育儿知识 作业答案 考试题库 百科 知识分享

C的Stack滴练习的定义部分,那位帮忙纠错#include"stdio.h"#include"stdlib.h"#defineStackInitSize100#defineStackIncrement10#defineintint#defineOVERFLOW255//Stack类型

题目详情
C的Stack滴练习的定义部分,那位帮忙纠错
#include "stdio.h"
#include "stdlib.h"
#define Stack_Init_Size 100
#define StackIncrement 10
#define int int
#define OVERFLOW 255
//Stack类型
▼优质解答
答案和解析
你对指针和引用操作看来挺混乱的
指针使用元素时,要用->来连接
int InitStack(SqStack *s) //这里定义s是指针
{
s->base=(int*)malloc(Stack_Init_Size * sizeof (int)); //不能s.base,下同
if(!s->base) exit(OVERFLOW);
s->top=s->base;
s->stacksize=Stack_Init_Size; //这个是对的
return 0;
}
引用时,如果原变量不是指针,就要用.(点)来连接 若是指针就用->来连接.
以下帮你修改好了
int InitStack(SqStack *s) //这里定义s是指针
{
s->base=(int*)malloc(Stack_Init_Size * sizeof (int)); //不能s.base,下同
if(!s->base) exit(OVERFLOW);
s->top=s->base;
s->stacksize=Stack_Init_Size; //这个是对的
return 0;
}
int Push(SqStack *s, int e)
{
if(s->top - s->base >= s->stacksize){
s->base=(int *) realloc (s->base,
(s->stacksize + StackIncrement) * sizeof (int));
if(!s->base)exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize += StackIncrement;
}
*s->top++ =e;
return 0;
}
int Pop(SqStack *s,int *e){
if(s->top == s->base) return 1;
e = --s->top;
return 0;
}
int StackEmpty(SqStack *s){
if(s->top - s->base