package class03; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class Code07_TwoQueueImplementStack { public static class TwoQueueStack { public Queue queue; public Queue help; public TwoQueueStack() { queue = new LinkedList<>(); help = new LinkedList<>(); } public void push(T value) { queue.offer(value); } public T poll() { while (queue.size() > 1) { help.offer(queue.poll()); } T ans = queue.poll(); Queue tmp = queue; queue = help; help = tmp; return ans; } public T peek() { while (queue.size() > 1) { help.offer(queue.poll()); } T ans = queue.poll(); help.offer(ans); Queue tmp = queue; queue = help; help = tmp; return ans; } public boolean isEmpty() { return queue.isEmpty(); } } public static void main(String[] args) { System.out.println("test begin"); TwoQueueStack myStack = new TwoQueueStack<>(); Stack test = new Stack<>(); int testTime = 1000000; int max = 1000000; for (int i = 0; i < testTime; i++) { if (myStack.isEmpty()) { if (!test.isEmpty()) { System.out.println("Oops"); } int num = (int) (Math.random() * max); myStack.push(num); test.push(num); } else { if (Math.random() < 0.25) { int num = (int) (Math.random() * max); myStack.push(num); test.push(num); } else if (Math.random() < 0.5) { if (!myStack.peek().equals(test.peek())) { System.out.println("Oops"); } } else if (Math.random() < 0.75) { if (!myStack.poll().equals(test.pop())) { System.out.println("Oops"); } } else { if (myStack.isEmpty() != test.isEmpty()) { System.out.println("Oops"); } } } } System.out.println("test finish!"); } }