为什么调用push的时候无法打印出入栈成功呢
问题描述:
//顺序栈的基本操作
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MaxSize 10
#define ElemType int
//定义栈的存储类型
typedef struct Nodestack *Sqstack;
struct Nodestack{
ElemType data[MaxSize];//利用数组存储数据
int Top; //栈顶指针
};
//初始化栈
void InitStack(Sqstack s){
s->Top==-1;
printf("初始化完成\n");
}
//判断是否是空栈
bool Sqstackempty (Sqstack s){
if(s->Top==-1)
return true;
return false;
}
//进栈操作
bool Push(Sqstack s,ElemType x ){
//printf("入栈成功\n");
if(s->Top==MaxSize-1){
printf("栈满了 无法入栈\n");
return false;
}
s->data[++s->Top] = x;
printf("入栈成功\n");
return true;
}
//出栈操作
bool Pop(Sqstack s,ElemType x){
if(s->Top==-1){
printf("栈空\n");
return false;
}
x=s->data[s->Top--];
printf("将%d弹出栈",x);
return true;
}
//读取栈顶元素
bool GetTop(Sqstack s,ElemType x){
if(s->Top==-1){
printf("栈空\n");
return false;
}
x=s->data[s->Top];
return true;
}
int main(){
Sqstack S;
InitStack(S);
int x;
scanf("%d",&x);
bool flag;
flag = Push(S,x);
}
答
main函数中Sqstack S这里,S没有申请空间
Sqstack S = (Sqstack)malloc(sizeof(Nodestack));
InitStack(Sqstack s)函数中,s->Top=-1; //这里你多写了一个=