本次对栈的学习有以下重点
1. 栈的初始化一定要做,st.top==-1;
2. 进栈操作关键点,st.data[++st.top]=x;进栈判断:if(st.top==maxsize-1)
3. 出栈操作关键点:st.top--;
4. 判空操作条件:st.top==-1;
具体代码实现:
栈的定义:
typedef struct{
int data[maxSize];
int top;
}SqStack;
栈的初始化:
void initSqStack(SqStack &st){
st.top=-1;
}
栈的判空:
int isEmpty(SqStack &st){
if(st.top==-1)
return 1;
else
return 0;
}
进栈操作:
int push(SqStack &st,int x){
if(st.top==maxSize-1){
return 0;
}
else {
st.data[++st.top] = x;
return 1;
}
}
出栈操作:
int pop(SqStack &st){
if(isEmpty(st)==1){
return 0;
}
else{
st.top--;
return 1;
}
}
打印栈:
void printStack(SqStack st){
while(st.top!=-1){
printf("%d",st.data[st.top--]);
}
}