栈的创建和基本操作
1、 顺序栈的实现
#define MaxSize 50
Typedef struct{
Elemtype data[MaxSize]; //存放栈中元素
Int top; //栈顶指针
}SqStack;
栈顶指针:S.top,初始时设置S.top=-1; 栈顶元素:S.data[S.top]
进栈操作:栈不满时,栈顶指针先加1,再送值到栈顶元素。
出栈操作:栈非空时,先取栈顶元素值,再将栈顶指针减1
栈空条件:S.top==-1; 栈满条件:S.top==MaxSize-1; 栈长:S.top+1
2、 顺序栈基本运算
1、 初始化栈描述代码
Void InitStack(&S){
S.top=-1;
}
2、判断栈空
Bool StackEmpty(S){
If(s.top==-1)
Return true;
Else
Return false;
}
3、进栈
Bool Push(SqStack &S,ElemType x){
If(S.top==MaxSize-1) return false;
S.data[++S.top]=x;
Return true;
}
4、出栈
Bool Pop(SqStack &S,ElemType &x){
If(S.top==-1) return false;
x=S.data[S.top–];
return true;
}
5、读取栈顶元素
Bool GetTop(SqStack S,ElemType &x){
If(S.top==-1)
Return false;
x=S.data[S.top];
return true;
}
共享栈的概念
