|
| 1 | +--- |
| 2 | +id: queue-using-stacks |
| 3 | +title: Implement Queue using Stacks |
| 4 | +sidebar_label: Implement Queue using Stacks |
| 5 | +tags: |
| 6 | +- Data Structures |
| 7 | +- Stack |
| 8 | +- Queue |
| 9 | +- Python |
| 10 | +- Java |
| 11 | +- C++ |
| 12 | +description: "This document provides an implementation of a queue using stacks, supporting push, pop, peek, and empty operations." |
| 13 | +--- |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +``` |
| 20 | +
|
| 21 | +Input |
| 22 | +["MyQueue", "push", "push", "peek", "pop", "empty"] |
| 23 | +[[], [1], [2], [], [], []] |
| 24 | +Output |
| 25 | +[null, null, null, 1, 1, false] |
| 26 | +
|
| 27 | +Explanation |
| 28 | +MyQueue myQueue = new MyQueue(); |
| 29 | +myQueue.push(1); // queue is: [1] |
| 30 | +myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) |
| 31 | +myQueue.peek(); // return 1 |
| 32 | +myQueue.pop(); // return 1, queue is [2] |
| 33 | +myQueue.empty(); // return false |
| 34 | +``` |
| 35 | + |
| 36 | +### Constraints |
| 37 | + |
| 38 | +- $1 <= x <= 9$ |
| 39 | +- At most 100 calls will be made to push, pop, peek, and empty. |
| 40 | +- All the calls to pop and peek are valid. |
| 41 | + |
| 42 | +## Problem Statement |
| 43 | +Implement a first-in-first-out (FIFO) queue using two stacks. The implemented queue should support the following operations: |
| 44 | +- `push(x)`: Pushes element `x` to the back of the queue. |
| 45 | +- `pop()`: Removes the element from the front of the queue and returns it. |
| 46 | +- `peek()`: Returns the element at the front of the queue without removing it. |
| 47 | +- `empty()`: Returns whether the queue is empty. |
| 48 | + |
| 49 | +## Approach |
| 50 | +To achieve FIFO order using stacks, we can use two stacks: |
| 51 | +- `stack1`: Used for enqueue operations (`push`). |
| 52 | +- `stack2`: Used for dequeue operations (`pop` and `peek`). |
| 53 | + |
| 54 | +### Operations Details: |
| 55 | +1. **Push Operation (`push(x)`)**: |
| 56 | + - Simply push the element onto `stack1`. |
| 57 | + |
| 58 | +2. **Pop Operation (`pop()`)**: |
| 59 | + - If `stack2` is empty, transfer all elements from `stack1` to `stack2`. Then, pop the top element from `stack2`. |
| 60 | + |
| 61 | +3. **Peek Operation (`peek()`)**: |
| 62 | + - If `stack2` is empty, transfer all elements from `stack1` to `stack2`. Then, peek at the top element of `stack2`. |
| 63 | + |
| 64 | +4. **Empty Check (`empty()`)**: |
| 65 | + - The queue is considered empty if both `stack1` and `stack2` are empty. |
| 66 | + |
| 67 | +#### Code in Different Languages |
| 68 | + |
| 69 | +<Tabs> |
| 70 | + <TabItem value="Python" label="Python" default> |
| 71 | + <SolutionAuthor name="@mahek0620"/> |
| 72 | + ```python |
| 73 | + |
| 74 | + class MyQueue: |
| 75 | + |
| 76 | + def __init__(self): |
| 77 | + self.stack1 = [] # Stack for enqueue operations |
| 78 | + self.stack2 = [] # Stack for dequeue operations |
| 79 | + |
| 80 | + def push(self, x): |
| 81 | + """ |
| 82 | + Push element x to the back of the queue. |
| 83 | + """ |
| 84 | + self.stack1.append(x) |
| 85 | + |
| 86 | + def pop(self): |
| 87 | + """ |
| 88 | + Removes the element from the front of the queue and returns it. |
| 89 | + """ |
| 90 | + self.move_elements() |
| 91 | + return self.stack2.pop() |
| 92 | + |
| 93 | + def peek(self): |
| 94 | + """ |
| 95 | + Returns the element at the front of the queue without removing it. |
| 96 | + """ |
| 97 | + self.move_elements() |
| 98 | + return self.stack2[-1] |
| 99 | + |
| 100 | + def empty(self): |
| 101 | + """ |
| 102 | + Returns whether the queue is empty. |
| 103 | + """ |
| 104 | + return len(self.stack1) == 0 and len(self.stack2) == 0 |
| 105 | + |
| 106 | + def move_elements(self): |
| 107 | + """ |
| 108 | + Moves elements from stack1 to stack2 if stack2 is empty. |
| 109 | + """ |
| 110 | + if not self.stack2: |
| 111 | + while self.stack1: |
| 112 | + self.stack2.append(self.stack1.pop()) |
| 113 | + |
| 114 | + ``` |
| 115 | + </TabItem> |
| 116 | + <TabItem value="Java" label="Java"> |
| 117 | + <SolutionAuthor name="@mahek0620"/> |
| 118 | + ```java |
| 119 | + |
| 120 | + import java.util.Stack; |
| 121 | + |
| 122 | + class MyQueue { |
| 123 | + private Stack<Integer> stack1; |
| 124 | + private Stack<Integer> stack2; |
| 125 | + |
| 126 | + /** Initialize your data structure here. */ |
| 127 | + public MyQueue() { |
| 128 | + stack1 = new Stack<>(); |
| 129 | + stack2 = new Stack<>(); |
| 130 | + } |
| 131 | + |
| 132 | + /** Push element x to the back of queue. */ |
| 133 | + public void push(int x) { |
| 134 | + stack1.push(x); |
| 135 | + } |
| 136 | + |
| 137 | + /** Removes the element from in front of queue and returns that element. */ |
| 138 | + public int pop() { |
| 139 | + moveElements(); |
| 140 | + return stack2.pop(); |
| 141 | + } |
| 142 | + |
| 143 | + /** Get the front element. */ |
| 144 | + public int peek() { |
| 145 | + moveElements(); |
| 146 | + return stack2.peek(); |
| 147 | + } |
| 148 | + |
| 149 | + /** Returns whether the queue is empty. */ |
| 150 | + public boolean empty() { |
| 151 | + return stack1.isEmpty() && stack2.isEmpty(); |
| 152 | + } |
| 153 | + |
| 154 | + private void moveElements() { |
| 155 | + if (stack2.isEmpty()) { |
| 156 | + while (!stack1.isEmpty()) { |
| 157 | + stack2.push(stack1.pop()); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + ``` |
| 164 | + </TabItem> |
| 165 | + <TabItem value="C++" label="C++"> |
| 166 | + <SolutionAuthor name="@mahek0620"/> |
| 167 | + ```cpp |
| 168 | + |
| 169 | + #include <stack> |
| 170 | + |
| 171 | + class MyQueue { |
| 172 | + private: |
| 173 | + std::stack<int> stack1; |
| 174 | + std::stack<int> stack2; |
| 175 | + |
| 176 | + public: |
| 177 | + /** Initialize your data structure here. */ |
| 178 | + MyQueue() { |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + /** Push element x to the back of queue. */ |
| 183 | + void push(int x) { |
| 184 | + stack1.push(x); |
| 185 | + } |
| 186 | + |
| 187 | + /** Removes the element from in front of queue and returns that element. */ |
| 188 | + int pop() { |
| 189 | + moveElements(); |
| 190 | + int front = stack2.top(); |
| 191 | + stack2.pop(); |
| 192 | + return front; |
| 193 | + } |
| 194 | + |
| 195 | + /** Get the front element. */ |
| 196 | + int peek() { |
| 197 | + moveElements(); |
| 198 | + return stack2.top(); |
| 199 | + } |
| 200 | + |
| 201 | + /** Returns whether the queue is empty. */ |
| 202 | + bool empty() { |
| 203 | + return stack1.empty() && stack2.empty(); |
| 204 | + } |
| 205 | + |
| 206 | + private: |
| 207 | + void moveElements() { |
| 208 | + if (stack2.empty()) { |
| 209 | + while (!stack1.empty()) { |
| 210 | + stack2.push(stack1.top()); |
| 211 | + stack1.pop(); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + }; |
| 216 | + |
| 217 | + ``` |
| 218 | + |
| 219 | + </TabItem> |
| 220 | +</Tabs> |
| 221 | + |
| 222 | + |
| 223 | +## References |
| 224 | + |
| 225 | +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/implement-queue-using-stacks/) |
| 226 | +- **Solution Link:** [Implement-Queue-Using-Stacks Solution on LeetCode](https://leetcode.com/problems/implement-queue-using-stacks/solutions/) |
| 227 | +- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
0 commit comments