剑指offer第五题
/**
* 用两个栈来实现一个队列,完成队列的Push和Pop操作。
* 队列中的元素为int类型
* @date 2020/8/9
**/
public class Test05 {
//进
static Stack<Integer> stack1 = new Stack<Integer>();
//出
static Stack<Integer> stack2 = new Stack<Integer>();
public static void push(int node) {
stack1.push(node);
}
public static int pop() {
//把stack1中的元素移到stack2中
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
//拿到元素
int result = stack2.pop();
//把stack2中的元素放回stack1中
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return result;
}
}