栈是一种线性表
#include<stdio.h>
#include<stdlib.h>
#define maxSize 100
typedef struct{
int data[maxSize];
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 x){
if(s.top==maxSize-1){ //栈满
return false;
}
s.top++;
s.data[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--; //<==>x=s.data[s.top--]
return true;
}
示意图: