0.定义
#define MaxSize 50
typedef struct {
Elemtype data[MaxSize];
int top;
}SqStack;
1.初始化
void InitStack(SqStack &S) {
S.top = -1;
}
2.判空
bool StackEmpty(SqStack 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;
}
②实现二:栈顶指针指向栈顶元素下一位置
bool Push(SqStack &S, ElemType x) {
if(S.top == MaxSize)
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;
}
②实现二:栈顶指针指向栈顶元素下一位置
bool Pop(SqStack &S, ElemType &x) {
if(S.top == 0)
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;
}
②实现二:栈顶指针指向栈顶元素下一位置
bool GetTop(SqStack S, ElemType &x) {
if(S.top == 0)
return false;
x = S.data[S.top-1];
return true;
}