一个简单的堆栈问题
写出下列程序段的输出结果(栈的元素类型 SElemType 为 char)。
void main(){
Stack S;
char x,y;
InitStack(S);
x='c';y='k';
Push(S,x);Push(S,'a');Push(S,y);
Pop(S,x);Push(S,'t');Push(S,x);
Pop(S,x);Push(S,'s');
while(! StackEmpty(S)){Pop(S.y);printf(y);};
printf(x);
}
答案为stack
新手不理解,希望大家解释下。
[解决办法]
x='c'; y='k';Push(S,x); // cPush(S,'a'); // a cPush(S,y); // k a cPop(S,x); // a cPush(S,'t'); // t a cPush(S,x); // k t a cPop(S,x); // t a cPush(S,'s'); // s t a cwhile(!StackEmpty(S)){Pop(S,y);printf(y);}; // s t a cprintf(x); // k
[解决办法]
x='c'; y='k';
Push(S,x); // c
Push(S,'a'); // a c
Push(S,y); // k a c
Pop(S,x); // a c x=k
Push(S,'t'); // t a c
Push(S,x); // k t a c x=k
Pop(S,x); // t a c
Push(S,'s'); // s t a c
printf(y); //打印s t a c
printf(x); //打印k