顺序栈的存储结构

#define MaxSize 100
typedef int ElemType;
struct SqStack {
    ElemType data[MaxSize];
    int top;
};

初始化栈的算法

void InitStack(SqStack &st) {
    st.top=-1;
}

判断栈空的算法

int StackEmpty(SqStack st) {
    return (st.top==-1);
}

进栈算法

int Push(SqStack &st,ElemType x) {
    if(st.top==MaxSize-1)
        return 0;
    st.top++;
    st.data[st.top]=x;
    return 1;
}

出栈算法

int Pop(SqStack &st,ElemType &x) {
    if(st.top==-1)
        return 0;
    x=st.data[st.top];
    st.top--;
    return 1;
}

取栈顶元素算法

int GetTop(SqStack &st,ElemType &x) {
    if(st.top==-1)
        return 0;
    x=st.data[st.top];
    return 1;
}