栈和队列
栈
只允许一端进行插入或删除的线性表
栈顶,允许进行插入和删除
栈底,固定不变
按照先进后出的原则
顺序栈
栈是线性表的特例,栈的顺序存储结构也叫顺序栈
#define MaxSize 50 //定义栈中元素的最大个数
typedef struct{
Elemtype data[MaxSize]; //存放栈中元素
int top; //栈顶指针
} SqStack; //顺序栈的简写
【注意】
1.top值不能超过MaxSize
2.空为top== -1,满栈为top==Maxsize-1,栈的数据元素个数为top-1
1.判空
bool StackEmpty(SqStack S){
if(s.top==-1) return true;
else return false;
}
2.进栈
bool Push(SqStack &S ,ElemType x){
if(S.top==MaxSize-1) return false;
S.data[++S.top]=x; return true;
}
3.出栈
bool Pop(SqStack &S,ElemType &x){
if(S.top==-1) return false;
x=S.data[S.top--];
return true;
}
4.读取栈顶元素
bool GetTop(SqStack S,ElemType &x){
if(S.top==-1) return false;
x=S.data[S.top];
return true;
}
共享栈
#define MaxSize 100 //定义栈中元素的最大个数
typedef struct{
Elemtype data[MaxSize]; //存放栈中元素
int top1; //栈1栈