https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
思路:将数据从一个栈中捯饬到另一个栈中,模拟队列先进先出。
public class Implementing_queues_with_two_stacks {
static Stack<Integer> stack1 = new Stack<Integer>();
static Stack<Integer> stack2 = new Stack<Integer>();
public static void main(String[] args) {
push(1);
push(2);
System.out.println(pop());
System.out.println(pop());
}
public static void push(int node) {
stack1.push(node);
}
public static int pop() {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
int ans = stack2.pop();
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return ans;
}
}