一、结构体
//顺序栈的结构体
#define MaxSize 100
typedef struct{
int data[MaxSize];
int top;
}SqStack;
二、初始化
代码如下(示例):
//初始化
void InitStack(SqStack &st){
st.top = -1;
}
三、判空
代码如下(示例):
//判空
bool IsEmpty(SqStack st){
if (st.top == -1)
return true;
else
return false;
}
四、入栈
代码如下(示例):
//入栈
bool InStack(SqStack &st, int x){
if (st.top == MaxSize - 1)
return false;
st.data[++st.top] = x;
return true;
}
五、出栈
代码如下(示例):
//出栈
bool OutStack(SqStack &st, int &x){
if (st.top == -1)
return false;
x = st.data[st.top--];
return true;
}