-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
76ae21c
commit efd9c83
Showing
1 changed file
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
# 13. Implement a Queue by using Stack | ||
|
||
### Problem | ||
|
||
https://bigfrontend.dev/problem/implement-a-queue-by-using-stack | ||
|
||
# | ||
|
||
### Problem Description | ||
|
||
In JavaScript, we could use array to work as both a Stack or a queue. | ||
|
||
```js | ||
const arr = [1, 2, 3, 4]; | ||
|
||
arr.push(5); // now array is [1, 2, 3, 4, 5] | ||
arr.pop(); // 5, now the array is [1, 2, 3, 4] | ||
``` | ||
|
||
Above code is a Stack, while below is a Queue | ||
|
||
```js | ||
const arr = [1, 2, 3, 4]; | ||
|
||
arr.push(5); // now the array is [1, 2, 3, 4, 5] | ||
arr.shift(); // 1, now the array is [2, 3, 4, 5] | ||
``` | ||
|
||
now suppose you have a stack, which has only follow interface: | ||
|
||
```js | ||
class Stack { | ||
push(element) { | ||
/* add element to stack */ | ||
} | ||
peek() { | ||
/* get the top element */ | ||
} | ||
pop() { | ||
/* remove the top element */ | ||
} | ||
size() { | ||
/* count of elements */ | ||
} | ||
} | ||
``` | ||
|
||
Could you implement a Queue by using only above Stack? A Queue must have following interface | ||
|
||
```js | ||
class Queue { | ||
enqueue(element) { | ||
/* add element to queue, similar to Array.prototype.push */ | ||
} | ||
peek() { | ||
/* get the head element */ | ||
} | ||
dequeue() { | ||
/* remove the head element, similar to Array.prototype.pop */ | ||
} | ||
size() { | ||
/* count of elements */ | ||
} | ||
} | ||
``` | ||
|
||
**notes** | ||
|
||
you can only use Stack as provided, Array should be avoided for the purpose of practicing. | ||
|
||
# | ||
|
||
### Solution 1 (O(1) enqueue, O(n) dequeue and peek) | ||
|
||
```js | ||
/* you can use this Class which is bundled together with your code | ||
class Stack { | ||
push(element) { // add element to stack } | ||
peek() { // get the top element } | ||
pop() { // remove the top element} | ||
size() { // count of element } | ||
} | ||
*/ | ||
|
||
/* Array is disabled in your code */ | ||
|
||
// you need to complete the following Class | ||
class Queue { | ||
constructor() { | ||
this.content = new Stack(); | ||
} | ||
|
||
enqueue(element) { | ||
this.content.push(element); | ||
} | ||
peek() { | ||
const reversedQueue = this._reverse(this.content); | ||
return reversedQueue.peek(); | ||
} | ||
size() { | ||
return this.content.size(); | ||
} | ||
dequeue() { | ||
const reversedQueue = this._reverse(this.content); | ||
const poppedItem = reversedQueue.pop(); | ||
this.content = this._reverse(reversedQueue); | ||
return poppedItem; | ||
} | ||
_reverse(queue) { | ||
const reversedQueue = new Stack(); | ||
while (queue.size() > 0) { | ||
const lastItem = queue.pop(); | ||
reversedQueue.push(lastItem); | ||
} | ||
|
||
return reversedQueue; | ||
} | ||
} | ||
``` | ||
|
||
# | ||
|
||
### Solution 2 (O(n) enqueue, O(1) dequeue and peek) | ||
|
||
```js | ||
/* you can use this Class which is bundled together with your code | ||
class Stack { | ||
push(element) { // add element to stack } | ||
peek() { // get the top element } | ||
pop() { // remove the top element} | ||
size() { // count of element } | ||
} | ||
*/ | ||
|
||
/* Array is disabled in your code */ | ||
|
||
// you need to complete the following Class | ||
class Queue { | ||
constructor() { | ||
this.content = new Stack(); | ||
this.reversedContent = new Stack(); | ||
} | ||
|
||
enqueue(element) { | ||
while (this.content.size() > 0) { | ||
const poppedItem = this.content.pop(); | ||
this.reversedContent.push(poppedItem); | ||
} | ||
|
||
this.reversedContent.push(element); | ||
|
||
while (this.reversedContent.size() > 0) { | ||
const poppedItem = this.reversedContent.pop(); | ||
this.content.push(poppedItem); | ||
} | ||
} | ||
peek() { | ||
return this.content.peek(); | ||
} | ||
size() { | ||
return this.content.size(); | ||
} | ||
dequeue() { | ||
return this.content.pop(); | ||
} | ||
} | ||
``` |