C语言 。数据结构。编写的程序在入栈那显示错误

2024-11-09 05:58:22
推荐回答(2个)
回答1:

/*
87 9 10 56 43 90 6 54 34 12
请按任意键继续. . .
*/
#include
#include
typedef int elemtype;
typedef int status;
#define OK 1
#define ERROR -1
#define OVERFLOW 0

int stack_size = 100;int stack_increasement = 10;
struct Sqstack {
elemtype *top;//栈顶元素
elemtype *base; //栈底元素
int stacksize;//栈的大小
};

//①初始化栈
status initstack(struct Sqstack &s) {
s.base = (elemtype *)malloc(stack_size*sizeof(elemtype));
if(!s.base) return OVERFLOW;
s.top = s.base;
s.stacksize = stack_size;
return OK;
}
//②入栈
status push(struct Sqstack &s,elemtype e) { //提示的是这行
if(s.top - s.base >= s.stacksize) {
s.base = (elemtype *)realloc(s.base,(s.stacksize+stack_increasement)*sizeof(elemtype));
if(!(s.base)) return OVERFLOW;
s.top = s.base + s.stacksize;
s.stacksize += stack_increasement;
}
*s.top++ = e;
return OK;
}
//③出栈
elemtype pop(struct Sqstack&s) {
elemtype e;
if(s.top == s.base) return ERROR;
e = *--s.top;
return e;
}
//④取栈顶元素
elemtype gettop(struct Sqstack &s) {
elemtype e;
if(s.top == s.base) return ERROR;
e = *(s.top - 1);
return e;
}

int main() {
elemtype a[] = {12,34,54,6,90,43,56,10,9,87};
struct Sqstack s;
int i,n = sizeof(a)/sizeof(a[0]);
initstack(s);
for(i = 0;i < n;i++) push(s,a[i]);
for(i = 0;i < n;i++) printf("%d ",pop(s));
printf("\n");
system("pause");
return 0;
}

回答2:

提示错误信息是什么?
表面上看不出有明显的错误