#include <stdio.h>
#define M 50
typedef struct node{
int data[M];
int top;
}SqStack;
void InitStack(SqStack &S){
S.top=-1;
}
bool StackEmpty(SqStack &S){
if(S.top==-1){
return true;
}
else{
return false;
}
}
bool Push(SqStack &S,int e){ //入栈
if(S.top==M-1){ //栈满报错
return false;
}
S.data[++S.top]=e;
return true;
}
bool Pop(SqStack &S,int &x){ //出栈
if(S.top==-1){ //栈空报错
return false;
}
x=S.data[S.top--];
return true;
}
bool GetTop(SqStack &S,int &x){ //取栈顶
if(S.top==-1){ //栈空报错
return false;
}
x=S.data[S.top]; //与出栈区别
}
void test(){
SqStack S;
InitStack(S);
if(StackEmpty(S)){
printf("栈空\n");
}
else{
printf("栈非空\n");
}
Push(S,1);
Push(S,2);
Push(S,3);
StackEmpty(S);
int a;
Pop(S,a);
GetTop(S,a);
printf("栈顶元素: %d",a);
}
int main() {
test();
return 0;
}
顺序栈C语言实现
最新推荐文章于 2024-07-25 19:28:17 发布