|
| 1 | +# Queue in JavaScript |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://youtu.be/Pq_D3wiN4k8"> |
| 5 | + <img src="https://img.youtube.com/vi/Pq_D3wiN4k8/0.jpg" alt="Queue in JavaScript" /> |
| 6 | + </a> |
| 7 | +</p> |
| 8 | + |
| 9 | +## Queue Implementation Using Array |
| 10 | + |
| 11 | +```javascript |
| 12 | +class Queue{ |
| 13 | + constructor(){ |
| 14 | + this.queue = [] |
| 15 | + } |
| 16 | + |
| 17 | + enqueue(data){ |
| 18 | + this.queue.push(data) |
| 19 | + } |
| 20 | + |
| 21 | + dequeue(){ |
| 22 | + return this.isEmpty() ? null : this.queue.shift() |
| 23 | + } |
| 24 | + |
| 25 | + front(){ |
| 26 | + return this.isEmpty() ? null : this.queue.at(0) |
| 27 | + } |
| 28 | + |
| 29 | + back(){ |
| 30 | + return this.isEmpty() ? null : this.queue.at(-1) |
| 31 | + } |
| 32 | + |
| 33 | + isEmpty(){ |
| 34 | + return this.queue.length === 0; |
| 35 | + } |
| 36 | + |
| 37 | + size(){ |
| 38 | + return this.queue.length |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const queue = new Queue() |
| 43 | +queue.enqueue(1) |
| 44 | +queue.enqueue(2) |
| 45 | +queue.enqueue(3) |
| 46 | +console.log(queue.dequeue()) // 1 |
| 47 | +console.log(queue.front()) // 2 |
| 48 | +console.log(queue.back()) // 3 |
| 49 | +console.log(queue.isEmpty()) // false |
| 50 | +console.log(queue.size()) // 2 |
| 51 | +console.log(queue) // Queue { queue: [2, 3]} |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +## Queue Implementation Using Linked List |
| 56 | + |
| 57 | +```javascript |
| 58 | +class Node{ |
| 59 | + constructor(data){ |
| 60 | + this.data = data; |
| 61 | + this.next = null; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class QueueLinkedList{ |
| 66 | + constructor(){ |
| 67 | + this.head = null; |
| 68 | + this.tail = null; |
| 69 | + this.size = 0; |
| 70 | + } |
| 71 | + |
| 72 | + enqueue(data){ |
| 73 | + const newNode = new Node(data); |
| 74 | + |
| 75 | + if(this.head === null){ |
| 76 | + this.head = newNode; |
| 77 | + } else{ |
| 78 | + this.tail.next = newNode; |
| 79 | + } |
| 80 | + |
| 81 | + this.tail = newNode; |
| 82 | + this.size++; |
| 83 | + } |
| 84 | + |
| 85 | + dequeue(){ |
| 86 | + if(this.isEmpty()){ |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + const deletedItem = this.head.data; |
| 91 | + this.head = this.head.next; |
| 92 | + this.size--; |
| 93 | + return deletedItem; |
| 94 | + } |
| 95 | + |
| 96 | + front(){ |
| 97 | + return this.isEmpty() ? null : this.head.data; |
| 98 | + } |
| 99 | + |
| 100 | + back(){ |
| 101 | + return this.isEmpty() ? null : this.tail.data; |
| 102 | + } |
| 103 | + |
| 104 | + isEmpty(){ |
| 105 | + return this.size === 0; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +const queue1 = new QueueLinkedList() |
| 110 | +queue1.enqueue(5) |
| 111 | +queue1.enqueue(6) |
| 112 | +queue1.enqueue(7) |
| 113 | +console.log(queue1.dequeue()) // 5 |
| 114 | +console.log(queue1.front()) // 6 |
| 115 | +console.log(queue1.back()) // 7 |
| 116 | +console.log(queue1.size) // 2 |
| 117 | +console.log(queue1) |
| 118 | +/* QueueLinkedList |
| 119 | +{ |
| 120 | + head: Node { data: 6, next: Node { data: 7, next: null }}, |
| 121 | + tail: Node{data: 7, next: null}, |
| 122 | + size: 2 |
| 123 | +} |
| 124 | +*/ |
| 125 | +``` |
| 126 | + |
| 127 | +## Implement Queue Using Stacks |
| 128 | + |
| 129 | +```javascript |
| 130 | +class QueueStack{ |
| 131 | + constructor(){ |
| 132 | + this.stack1 = [] |
| 133 | + this.stack2 = [] |
| 134 | + } |
| 135 | + |
| 136 | + push(x){ |
| 137 | + while(this.stack1.length > 0){ |
| 138 | + this.stack2.push(this.stack1.pop()) |
| 139 | + } |
| 140 | + |
| 141 | + this.stack1.push(x); |
| 142 | + |
| 143 | + while(this.stack2.length > 0){ |
| 144 | + this.stack1.push(this.stack2.pop()) |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + pop(){ |
| 149 | + if(this.empty()){ |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + return this.stack1.pop() |
| 154 | + }; |
| 155 | + |
| 156 | + peek(){ |
| 157 | + return this.empty() ? null : this.stack1.at(-1) |
| 158 | + }; |
| 159 | + |
| 160 | + empty(){ |
| 161 | + return this.stack1.length === 0 |
| 162 | + }; |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +## Implement Circular Queue Using Linked List |
| 167 | + |
| 168 | +```javascript |
| 169 | +class Node { |
| 170 | + constructor(data) { |
| 171 | + this.data = data; |
| 172 | + this.next = null; |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +class MyCircularQueue { |
| 177 | + constructor(k) { |
| 178 | + this.capacity = k; |
| 179 | + this.head = null; |
| 180 | + this.tail = null; |
| 181 | + this.size = 0; |
| 182 | + } |
| 183 | + |
| 184 | + enQueue(data) { |
| 185 | + if(this.isFull()){ |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + const newNode = new Node(data); |
| 190 | + |
| 191 | + if(this.head === null){ |
| 192 | + this.head = newNode; |
| 193 | + } else{ |
| 194 | + this.tail.next = newNode; |
| 195 | + } |
| 196 | + |
| 197 | + this.tail = newNode; |
| 198 | + this.tail.next = this.head; |
| 199 | + this.size++; |
| 200 | + return true; |
| 201 | + } |
| 202 | + |
| 203 | + deQueue() { |
| 204 | + if(this.isEmpty()){ |
| 205 | + return false; |
| 206 | + } |
| 207 | + |
| 208 | + if(this.head === this.tail){ |
| 209 | + this.head = null; |
| 210 | + this.tail = null; |
| 211 | + } else{ |
| 212 | + this.head = this.head.next; |
| 213 | + this.tail.next = this.head; |
| 214 | + } |
| 215 | + |
| 216 | + this.size--; |
| 217 | + return true; |
| 218 | + } |
| 219 | + |
| 220 | + Front() { |
| 221 | + return this.isEmpty() ? -1 : this.head.data; |
| 222 | + } |
| 223 | + |
| 224 | + Rear() { |
| 225 | + return this.isEmpty() ? -1 : this.tail.data; |
| 226 | + } |
| 227 | + |
| 228 | + isEmpty() { |
| 229 | + return this.size === 0; |
| 230 | + } |
| 231 | + |
| 232 | + isFull() { |
| 233 | + return this.size === this.capacity; |
| 234 | + } |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +## Practice Questions |
| 239 | + |
| 240 | +1. [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) |
| 241 | +2. [Implement Stacks using Queue](https://leetcode.com/problems/implement-stack-using-queues/) |
| 242 | +3. [Design Circular Queue](https://leetcode.com/problems/design-circular-queue/) |
| 243 | +4. [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) |
| 244 | +5. [Design Circular Deque](https://leetcode.com/problems/design-circular-deque/) |
0 commit comments