题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

  1. import java.util.Stack;
  2. public class Solution {
  3. static Stack<Integer> stack1 = new Stack<>();
  4. static Stack<Integer> stack2 = new Stack<>();
  5. /**
  6. * 队列入队
  7. *
  8. * @param node
  9. */
  10. public void push(int node) {
  11. stack1.push(node);
  12. }
  13. /**
  14. * 队列出队
  15. */
  16. public int pop() {
  17. while (!stack1.isEmpty()) {
  18. stack2.push(stack1.pop());
  19. }
  20. Integer integer = stack2.pop();
  21. while (!stack2.isEmpty()) {
  22. stack1.push(stack2.pop());
  23. }
  24. return integer;
  25. }
  26. }