剑指09 用两个栈实现队列

  1. class CQueue {
  2. private Deque<Integer> stack1;
  3. private Deque<Integer> stack2;
  4. public CQueue() {
  5. stack1 = new LinkedList<Integer>();
  6. stack2 = new LinkedList<Integer>();
  7. }
  8. public void appendTail(int value) {
  9. stack1.push(value);
  10. }
  11. public int deleteHead() {
  12. if (stack1.isEmpty() && stack2.isEmpty())
  13. return -1;
  14. if (stack2.isEmpty())
  15. while (!stack1.isEmpty())
  16. stack2.push(stack1.pop());
  17. return stack2.pop();
  18. }
  19. }
  20. /**
  21. * Your CQueue object will be instantiated and called as such:
  22. * CQueue obj = new CQueue();
  23. * obj.appendTail(value);
  24. * int param_2 = obj.deleteHead();
  25. */