Implement the following operations of a queue using stacks.

    • push(x) — Push element x to the back of queue.
    • pop() — Removes the element from in front of queue.
    • peek() — Get the front element.
    • empty() — Return whether the queue is empty.

    Example:

    1. MyQueue queue = new MyQueue();
    2. queue.push(1);
    3. queue.push(2);
    4. queue.peek(); // returns 1
    5. queue.pop(); // returns 1
    6. queue.empty(); // returns false

    Notes:

    • You must use only standard operations of a stack — which means only push to top, peek/pop from top, size, and is empty operations are valid.
    • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
    • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

    题意

    用栈来实现队列的功能。

    思路

    需要用到两个栈,有两种方法:

    方法1:只在stack1进行push;只在stack2进行pop和peek,当stack2空时,则将stack1中所有元素依次出栈并压入stack2中,再进行pop和peek。

    方法2:每次push新元素x时,将stack1中所有元素依次出栈并压入stack2中,将x压入stack1,再将stack2中所有元素依次出栈并压回stack1中,这样就能保证任何时候stack1要进行pop时都是先进先出。


    代码实现 - 方法1

    1. class MyQueue {
    2. Deque<Integer> stack1;
    3. Deque<Integer> stack2;
    4. /** Initialize your data structure here. */
    5. public MyQueue() {
    6. stack1 = new ArrayDeque<>();
    7. stack2 = new ArrayDeque<>();
    8. }
    9. /** Push element x to the back of queue. */
    10. public void push(int x) {
    11. stack1.push(x);
    12. }
    13. /** Removes the element from in front of queue and returns that element. */
    14. public int pop() {
    15. if (stack2.isEmpty()) {
    16. while (!stack1.isEmpty()) {
    17. stack2.push(stack1.pop());
    18. }
    19. }
    20. return stack2.pop();
    21. }
    22. /** Get the front element. */
    23. public int peek() {
    24. if (stack2.isEmpty()) {
    25. while (!stack1.isEmpty()) {
    26. stack2.push(stack1.pop());
    27. }
    28. }
    29. return stack2.peek();
    30. }
    31. /** Returns whether the queue is empty. */
    32. public boolean empty() {
    33. return stack1.isEmpty() && stack2.isEmpty();
    34. }
    35. }

    代码实现 - 方法2

    1. class MyQueue {
    2. Deque<Integer> stack1;
    3. Deque<Integer> stack2;
    4. /** Initialize your data structure here. */
    5. public MyQueue() {
    6. stack1 = new ArrayDeque<>();
    7. stack2 = new ArrayDeque<>();
    8. }
    9. /** Push element x to the back of queue. */
    10. public void push(int x) {
    11. while (!stack1.isEmpty()) {
    12. stack2.push(stack1.pop());
    13. }
    14. stack1.push(x);
    15. while (!stack2.isEmpty()) {
    16. stack1.push(stack2.pop());
    17. }
    18. }
    19. /** Removes the element from in front of queue and returns that element. */
    20. public int pop() {
    21. return stack1.pop();
    22. }
    23. /** Get the front element. */
    24. public int peek() {
    25. return stack1.peek();
    26. }
    27. /** Returns whether the queue is empty. */
    28. public boolean empty() {
    29. return stack1.isEmpty();
    30. }
    31. }