用队列实现栈

题目描述:请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(push、top、pop 和 empty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:双队列实现栈

使用2个队列firstQueue和secondQueue存储数据,具体方法说明如下:

  • push(int x):如果firstQueue为空,则将x存到secondQueue中,否则存到firstQueue中;
  • pop():如果firstQueue为空,则将secondQueue中的数据都取出然后依次存入firstQueue中只留一个作为栈顶元素取出并返回;否则,将firstQueue中的数据都取出然后依次存入secondQueue中只留一个作为栈顶元素取出并返回;
  • top():逻辑通pop()方法类似;
  • empty():如果firstQueue和secondQueue都为空,返回true;否则,返回false。
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. public class LeetCode_225 {
  4. public static void main(String[] args) {
  5. MyStack myStack = new MyStack();
  6. myStack.push(1);
  7. myStack.push(2);
  8. myStack.push(3);
  9. System.out.println(myStack.top()); // 返回 2
  10. System.out.println(myStack.pop()); // 返回 2
  11. System.out.println(myStack.empty()); // 返回 False
  12. }
  13. }
  14. class MyStack {
  15. private Queue<Integer> firstQueue;
  16. private Queue<Integer> secondQueue;
  17. /**
  18. * Initialize your data structure here.
  19. */
  20. public MyStack() {
  21. firstQueue = new LinkedList<>();
  22. secondQueue = new LinkedList<>();
  23. }
  24. /**
  25. * Push element x onto stack.
  26. */
  27. public void push(int x) {
  28. if (firstQueue == null) {
  29. secondQueue.add(x);
  30. } else {
  31. firstQueue.add(x);
  32. }
  33. }
  34. /**
  35. * Removes the element on top of the stack and returns that element.
  36. */
  37. public int pop() {
  38. if (this.empty()) {
  39. throw new RuntimeException("empty stack.");
  40. }
  41. if (firstQueue.isEmpty()) {
  42. int size = secondQueue.size();
  43. for (int i = 1; i < size; i++) {
  44. firstQueue.add(secondQueue.poll());
  45. }
  46. return secondQueue.poll();
  47. } else {
  48. int size = firstQueue.size();
  49. for (int i = 1; i < size; i++) {
  50. secondQueue.add(firstQueue.poll());
  51. }
  52. return firstQueue.poll();
  53. }
  54. }
  55. /**
  56. * Get the top element.
  57. */
  58. public int top() {
  59. if (this.empty()) {
  60. throw new RuntimeException("empty stack.");
  61. }
  62. int result = -1;
  63. if (firstQueue.isEmpty()) {
  64. int size = secondQueue.size();
  65. for (int i = 1; i < size; i++) {
  66. firstQueue.add(secondQueue.poll());
  67. }
  68. result = secondQueue.peek();
  69. firstQueue.add(secondQueue.poll());
  70. } else {
  71. int size = firstQueue.size();
  72. for (int i = 1; i < size; i++) {
  73. secondQueue.add(firstQueue.poll());
  74. }
  75. result = firstQueue.peek();
  76. secondQueue.add(firstQueue.poll());
  77. }
  78. return result;
  79. }
  80. /**
  81. * Returns whether the stack is empty.
  82. */
  83. public boolean empty() {
  84. return firstQueue.isEmpty() && secondQueue.isEmpty();
  85. }
  86. }

【每日寄语】 哪怕生活不宠你,也要好好善待自己。这一生,风雨兼程,就是为了遇见最好的自己。