Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3122651
commit e8a9631
Showing
5 changed files
with
146 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
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,79 @@ | ||
/** | ||
* Queue from stack | ||
*/ | ||
|
||
// Class declaration for queue data structure | ||
class Queue { | ||
constructor(size) { | ||
this.first = new Stack(); | ||
this.second = new Stack(); | ||
} | ||
|
||
enqueue(element) { | ||
this.first.push(element); | ||
} | ||
|
||
dequeue() { | ||
// Empty first stack to second | ||
while (this.first.peek()) { | ||
this.second.push(this.first.pop()); | ||
} | ||
|
||
const element = this.second.pop(); | ||
|
||
// Restore first stack | ||
while (this.second.peek()) { | ||
this.first.push(this.second.pop()); | ||
} | ||
|
||
return element; | ||
} | ||
|
||
peek() { | ||
while (this.first.peek()) { | ||
this.second.push(this.first.pop()); | ||
} | ||
|
||
const element = this.second.peek(); | ||
|
||
while (this.second.peek()) { | ||
this.first.push(this.second.pop()); | ||
} | ||
|
||
return element; | ||
} | ||
} | ||
|
||
// Class declaration for stack data structure | ||
class Stack { | ||
constructor() { | ||
this.data = []; | ||
} | ||
|
||
push(record) { | ||
this.data.push(record); | ||
} | ||
|
||
pop() { | ||
return this.data.pop(); | ||
} | ||
|
||
peek() { | ||
return this.data[this.data.length - 1]; | ||
} | ||
} | ||
|
||
const q = new Queue(); | ||
|
||
q.enqueue(1); | ||
console.log(q.dequeue()); | ||
q.enqueue(1); | ||
q.enqueue(2); | ||
q.enqueue(3); | ||
console.log(q.dequeue()); | ||
console.log(q.dequeue()); | ||
console.log(q.dequeue()); | ||
q.enqueue(1); | ||
q.enqueue(2); | ||
console.log(q.peek()); | ||
console.log(q.peek()); |
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,66 @@ | ||
![cover](./cover.png) | ||
|
||
# Day 44 - Queue from Stacks | ||
|
||
Implement the Queue data structure using Stacks | ||
|
||
### Hint | ||
|
||
Use 2 stacks | ||
|
||
- enqueue operation same as push in a single stack | ||
- For dequeue, pop all the elements of first stack into second and apply pop once in second queue | ||
- for peek, again, pop all the elements of first stack into second and apply peek once in second queue | ||
|
||
Don't forget to restore the state of first stack after the dequeue and peek operations | ||
|
||
## Solution | ||
|
||
## JavaScript Implementation | ||
|
||
### [Solution](./JavaScript/queueFromStack.js) | ||
|
||
```js | ||
class Queue { | ||
constructor(size) { | ||
this.first = new Stack(); | ||
this.second = new Stack(); | ||
} | ||
|
||
enqueue(element) { | ||
this.first.push(element); | ||
} | ||
|
||
dequeue() { | ||
// Empty first stack to second | ||
while (this.first.peek()) { | ||
this.second.push(this.first.pop()); | ||
} | ||
|
||
const element = this.second.pop(); | ||
|
||
// Restore first stack | ||
while (this.second.peek()) { | ||
this.first.push(this.second.pop()); | ||
} | ||
|
||
return element; | ||
} | ||
|
||
peek() { | ||
while (this.first.peek()) { | ||
this.second.push(this.first.pop()); | ||
} | ||
|
||
const element = this.second.peek(); | ||
|
||
while (this.second.peek()) { | ||
this.first.push(this.second.pop()); | ||
} | ||
|
||
return element; | ||
} | ||
} | ||
``` | ||
|
||
[Click Here](./JavaScript/queueFromStack.js) for complete solution |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.