题目描述:使用栈实现队列的下列操作: push(x) — 将一个元素放入队列的尾部。 pop() — 从队列首部移除元素。 peek() — 返回队列首部的元素。 empty() — 返回队列是否为空。

    示例: MyQueue queue = new MyQueue();

    queue.push(1);

    queue.push(2);

    queue.peek(); // 返回 1

    queue.pop(); // 返回 1

    queue.empty(); // 返回 false

    说明:

    • 你只能使用标准的栈操作 — 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
    • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
    • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

    思路:
    做这道题大家首先要在心里清楚一个事情:栈和队列的区别在哪里?
    仔细想想,栈,后进先出;队列,先进先出。也就是说两者的进出顺序其实是反过来的。用栈实现队列,说白了就是用栈实现先进先出的效果,再说直接点,就是想办法让栈底的元素首先被取出,也就是让出栈序列被逆序
    乍一看有点头大:栈结构决定了栈底元素只能被死死地压在最底下,如何使它首先被取出呢?
    一个栈做不到的事情,我们用两个栈来做:

    1. /**
    2. * 初始化构造函数
    3. */
    4. const MyQueue = function () {
    5. // 初始化两个栈
    6. this.stack1 = [];
    7. this.stack2 = [];
    8. };
    9. /**
    10. * Push element x to the back of queue.
    11. * @param {number} x
    12. * @return {void}
    13. */
    14. MyQueue.prototype.push = function (x) {
    15. // 直接调度数组的 push 方法
    16. this.stack1.push(x);
    17. };
    18. /**
    19. * Removes the element from in front of queue and returns that element.
    20. * @return {number}
    21. */
    22. MyQueue.prototype.pop = function () {
    23. // 假如 stack2 为空,需要将 stack1 的元素转移进来
    24. if (this.stack2.length <= 0) {
    25. // 当 stack1 不为空时,出栈
    26. while (this.stack1.length !== 0) {
    27. // 将 stack1 出栈的元素推入 stack2
    28. this.stack2.push(this.stack1.pop());
    29. }
    30. }
    31. // 为了达到逆序的目的,我们只从 stack2 里出栈元素
    32. return this.stack2.pop();
    33. };
    34. /**
    35. * Get the front element.
    36. * @return {number}
    37. * 这个方法和 pop 唯一的区别就是没有将定位到的值出栈
    38. */
    39. MyQueue.prototype.peek = function () {
    40. if (this.stack2.length <= 0) {
    41. // 当 stack1 不为空时,出栈
    42. while (this.stack1.length != 0) {
    43. // 将 stack1 出栈的元素推入 stack2
    44. this.stack2.push(this.stack1.pop());
    45. }
    46. }
    47. // 缓存 stack2 的长度
    48. const stack2Len = this.stack2.length;
    49. return stack2Len && this.stack2[stack2Len - 1];
    50. };
    51. /**
    52. * Returns whether the queue is empty.
    53. * @return {boolean}
    54. */
    55. MyQueue.prototype.empty = function () {
    56. // 若 stack1 和 stack2 均为空,那么队列空
    57. return !this.stack1.length && !this.stack2.length;
    58. };