编辑代码

//3.1.1 顺序栈的定义
typedef struct
{
    ElemType data[MaxSize];     //存放栈中元素
    int top;                    //栈顶指针
}SqStack;                       //声明顺序栈类型

//3.1.2 初始化栈
void InitStack(SqStack &st)
{
    st.top = -1;
}

//3.1.3 判断栈是否为空
int StackEmpty(SqStack st)
{
    return(st.top == -1);
}

//3.1.4 进栈
int Push(SqStack &st,ElemType x)
{
    if(st.top == MaxSize - 1)   //栈满的情况
        return 0;
    st.top++;
    st.data[st.top] = x;
    return 1;
}

//3.1.5 出栈
int Pop(SqStack &st,ElemType x)
{
    if(st.top == -1)            //栈空的情况
        return 0;
    x = st.data[st.top];        //取栈顶元素
    return 1;
}

//取栈顶元素
int GetTop(SqStack &st,ElemType &x)
{
    if(st.top == -1)            //栈为空的情况
        return 0;
    x = st.data[st.top];        //取栈顶元素
    return 1;
}
//3.1.7 共享栈的定义
typedef struct
{
    ElemType data[MaxSize];
    int top1,top2;
}DStack;                        //声明共享栈类型