进栈操作:先移动指针,再进入元素
出栈操作:先取出元素,再移动指针
目录
1.初始化栈
void initStack(SqStack &st) //初始化栈
{
st.top = -1; //将栈顶指针设置为-1
}
2.判断栈空
栈st空的时候返回1,否则返回0
int isEmpty (SqStack st)
{
if(st.top == -1)
ruturn 1;
else
return 0;
}
3.进栈
//进栈
int push(SqStack &st, int x)
{
if(st.top == maxSize-1) //栈满不能进栈
return 0;
++(st.top); //先移动指针,再进入元素
st.data[st.top] = x;
return 1;
}
4.出栈
//出栈
int pop(SqStack &st, int &x)
{
if(st.top == -1) //栈空不能出栈
return 0;
x = st.data[st.top]; //先取出元素,再移动指针
--(st.top);
return 1;
}