问题遇到的现象和发生背景
顺序栈编写代码遭遇问题。
用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct
{
int *base; //鏍堝簳鎸囬拡
int *top; //鏍堥《鎸囬拡
int stacksize; //鏍堝彲鐢ㄧ殑鏈€澶у閲?
}SqStack;
//初始化
int InitStack(SqStack S);
//顺序栈入栈
int Push(SqStack S,int e);
//出栈
int Pop(SqStack S,int e);
//获得元素
int GetTop(SqStack S);
int main()
{
SqStack S;
int *e=1;
InitStack(S);
Push(S,e);
Push(S,e);
printf("%d",e);
int x = *S.top;
printf("%d",x);
printf("%d",x);
Pop(S,e);
GetTop(S);
return 0;
}
//初始化
int InitStack(SqStack S){
// S->base = (int)malloc(MAXSIZE*sizeof(int));
int arr[MAXSIZE];
S.base = arr;
if(!S.base) return 0;
S.top = S.base;
S.stacksize = MAXSIZE;
return 1;
}
//入栈
int Push(SqStack S,int e){
if(S.top-S.base == S.stacksize) return 0;
// *S.top++ = e;
*S.top = e;
S.top++;
return 1;
}
//出栈
int Pop(SqStack S,int e){
if(S.top == S.base) return 0;
// e = *--S.top;
S.top--;
e = *S.top;
return 1;
}
//获得元素
int GetTop(SqStack S){
if(S.base != S.top){
return *(S.top-1);
}
}
运行结果及报错内容
我的解答思路和尝试过的方法
经过调试,发现
*S.top = e;
S.top++;
出现问题
我想要达到的结果
希望能获得解决方案