typedef struct SqStack{
int data[maxSize];
int top;
}SqStack;
//顺序栈的初始化
void initStack(SqStack &st){
st.top = -1;
}
//判断栈空
int isEmpty(SqStack st){
return st.top == -1;
}
//进栈
int push(SqStack &st, int x){
if(st.top == maxSize)
return 0;
st.data[++st.top] = x;
return 1;
}
//出栈
int pop(SqStack &st, int &x){
if(st.top == -1)
return 0;
x = st.data[st.top--];
return 1;
}
//考试中顺序栈用法的简洁写法
int stack[maxSize]; int top = -1; //定义加初始化
stack[++top] = x; //进栈
x = stack[top--]; //出栈
【数据结构——栈和队列】顺序栈的基本操作
最新推荐文章于 2024-06-12 14:12:19 发布