英文原题:write a program to combine two stacks by placing all elements of the second stack on top of those in the first. The relative ordering of elements from the second stack is unchanged. Following the combine, the second stack is empty. (Hint: you can use push and pop methods of those stacks directly)
意思是:栈1存有 1,2,3,4,5;栈2存有6,7,8,9,10;如何让栈2中的原有数据保持顺序不变而放入栈1中;实现效果是:栈1存有1,2,3,4,5,6,7,8,9,10;栈2为空;
能贴上c++代码最好
1楼答案过于麻烦。只需要用到一个临时栈就行了。过程为:
创建临时栈
t
将
栈2
中的元素依次弹出到t
中,这时候t
的内容为10,9,8,7,6
将
t
中的元素再依次弹出到栈1
中,栈1
此时的顺序正好是从1到10