没啥可说的,搞清楚性质即可
/** * Created by itworker365 on 5/12/2017. */ public class TwoStack2Queue { public static void main(String[] args) { TwoStack2Queue twoStack2Queue = new TwoStack2Queue(); twoStack2Queue.queueTest(); } public void queueTest() { MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.push(3); System.out.println(queue.pop()); System.out.println(queue.pop()); queue.push(4); System.out.println(queue.pop()); queue.push(5); System.out.println(queue.pop()); System.out.println(queue.pop()); } class MyQueue { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.pop(); } } }