|  | 
 
| 
为啥我push函数每次输出的st.top都是0呀
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 源码:
 #include <stdio.h>
 #define MaxSize 100                                //顺序栈的初始分配空间大小
 typedef char ElemType;                        //假设顺序栈中所有元素为char类型
 typedef struct
 {        ElemType data[MaxSize];                //保存栈中元素
 int top;                                        //栈顶指针
 } SqStack;                                                //顺序栈类型
 
 void InitStack(SqStack &st)                //初始化顺序栈st
 {
 st.top=-1;
 }
 void DestroyStack(SqStack st)        //销毁顺序栈st
 {
 while(st.top!=-1)
 {
 
 }
 
 }
 
 int Push(SqStack st,ElemType x)        //进栈元素x
 {
 if(st.top == MaxSize -1)
 {
 printf("栈满");
 }
 else
 {
 st.top=st.top+1;
 printf("%d",st.top);
 st.data[st.top] = x;
 printf("ok\n");
 }
 }
 
 int Pop(SqStack &st,ElemType &x)        //出栈元素x
 {
 if(st.top==-1)
 printf("栈空");
 x = st.data[st.top];
 st.top--;
 printf("ok\n");
 }
 
 int GetTop(SqStack st,ElemType &x)        //取栈顶元素x
 {        if (st.top==-1)                                        //栈空
 return 0;
 else
 {        x=st.data[st.top];
 return 1;
 }
 }
 
 int StackEmpty(SqStack st)                //判断栈是否为空
 {        if (st.top==-1) return 1;
 else return 0;
 }
 int main()             //主函数
 {        SqStack st;
 ElemType e;
 printf("初始化栈st\n");
 InitStack(st);
 printf("栈%s\n",(StackEmpty(st)==1?"空":"不空"));
 printf("a进栈\n");Push(st,'a');
 printf("b进栈\n");Push(st,'b');
 printf("c进栈\n");Push(st,'c');
 printf("d进栈\n");Push(st,'d');
 printf("栈%s\n",(StackEmpty(st)==1?"空":"不空"));
 GetTop(st,e);
 printf("栈顶元素:%c\n",e);
 printf("出栈次序:");
 while (!StackEmpty(st))                //栈不空循环
 {        Pop(st,e);                                //出栈元素e并输出
 printf("%c ",e);
 }
 printf("\n销毁栈st\n");
 DestroyStack(st);
 }
 | 
 |