#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10 //定义栈中元素的最大个数
typedef struct{
int data[MaxSize]; //用静态的数组”存放栈中元素
int top; //栈顶指针
}SqStack; //顺序表的类型定义
int main(){
return 0;
}
//初始化栈
void InitStack(SqStack &S){
S.top = -1; //初始化栈顶指针
}
void testStack(){
SqStack S; //声明一个顺序栈(分配空间)
}
//判断栈空
bool StackEmpty(SqStack S){
if(S.top==-1){
return true; //栈空
}else{
return false; //不空
}
}
//新元素入栈
bool Push(SqStack &S,int x){
if(S.top==MaxSize-1){ //栈满,报错
return false;
}
S.top =S.top+1; //指针先加1
S.data[S.top] =x; //新元素入栈
//或者 S.data[++S.top] =x;
return true;
}
//出栈操作
bool Pop(SqStack &S,int &x){
if(S.top==-1){ //栈空,报错
return false;
}
x= S.data[S.top]; //栈顶元素先出栈
S.top =S.top-1; //指针再减1
//或者 x = S.data[S.top--];
return true;
}
//读栈操作
bool Pop(SqStack &S,int &x){
if(S.top==-1){ //栈空,报错
return false;
}
x= S.data[S.top]; //栈顶元素先出栈
return true;
}