先放代码
点击查看代码
#include <stdio.h>
#define Maxsize 50
#define true 1
#define false 0
typedef int Elemtype;
typedef struct{
Elemtype data[Maxsize];
int top;
}SqStack;
//初始化
void InitStack(SqStack *S){
S->top = -1;
}
//判空
int StackEmpty(SqStack S){
if(S.top==-1)
return true;
else
return false;
}
//进栈or压栈
int Push(SqStack *S,Elemtype x){
if(S->top==Maxsize-1) //栈满,报错
return false;
S->data[++S->top]=x; //指针先加1,再入栈
return true;
}
//出栈or弹栈
int Pop(SqStack *S,Elemtype x){
if(S->top==-1) //栈空,报错
return false;
x=S->data[S->top--]; //先出栈,指针再减一
return x;
}
//读栈顶元素
int GetTop(SqStack S,Elemtype x){
if(S.top==-1) //栈空,报错
return false;
x=S.data[S.top];
return x;
}
void S_p(SqStack S){
while(S.top>-1)
printf("%d ",S.data[S.top--]);
}
int main() {
SqStack S;
InitStack(&S);
Push(&S,0);Push(&S,1);Push(&S,2);S_p(S);
return 0;
}