Skip to content

Commit e8a9631

Browse files
committed
Add day 44
1 parent 3122651 commit e8a9631

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
7171
| [Day 41](./day41) | [Implement Queue Data Structure](./day41) | [http://codetoexpress.tech/dc/day41/](http://codetoexpress.tech/dc/day41/) | **Beginner** |
7272
| [Day 42](./day42) | [Alternate Queue Combination](./day42) | [http://codetoexpress.tech/dc/day42/](http://codetoexpress.tech/dc/day42/) | **intermediate** |
7373
| [Day 43](./day43) | [Queue Reversal](./day43) | [http://codetoexpress.tech/dc/day43/](http://codetoexpress.tech/dc/day43/) | **intermediate** |
74+
| [Day 44](./day44) | [Queue from Stacks](./day44) | [http://codetoexpress.tech/dc/day44/](http://codetoexpress.tech/dc/day44/) | **intermediate** |
7475

7576
## [More Problems](./BONUS/README.md)
7677

day44/JavaScript/queueFromStack.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Queue from stack
3+
*/
4+
5+
// Class declaration for queue data structure
6+
class Queue {
7+
constructor(size) {
8+
this.first = new Stack();
9+
this.second = new Stack();
10+
}
11+
12+
enqueue(element) {
13+
this.first.push(element);
14+
}
15+
16+
dequeue() {
17+
// Empty first stack to second
18+
while (this.first.peek()) {
19+
this.second.push(this.first.pop());
20+
}
21+
22+
const element = this.second.pop();
23+
24+
// Restore first stack
25+
while (this.second.peek()) {
26+
this.first.push(this.second.pop());
27+
}
28+
29+
return element;
30+
}
31+
32+
peek() {
33+
while (this.first.peek()) {
34+
this.second.push(this.first.pop());
35+
}
36+
37+
const element = this.second.peek();
38+
39+
while (this.second.peek()) {
40+
this.first.push(this.second.pop());
41+
}
42+
43+
return element;
44+
}
45+
}
46+
47+
// Class declaration for stack data structure
48+
class Stack {
49+
constructor() {
50+
this.data = [];
51+
}
52+
53+
push(record) {
54+
this.data.push(record);
55+
}
56+
57+
pop() {
58+
return this.data.pop();
59+
}
60+
61+
peek() {
62+
return this.data[this.data.length - 1];
63+
}
64+
}
65+
66+
const q = new Queue();
67+
68+
q.enqueue(1);
69+
console.log(q.dequeue());
70+
q.enqueue(1);
71+
q.enqueue(2);
72+
q.enqueue(3);
73+
console.log(q.dequeue());
74+
console.log(q.dequeue());
75+
console.log(q.dequeue());
76+
q.enqueue(1);
77+
q.enqueue(2);
78+
console.log(q.peek());
79+
console.log(q.peek());

day44/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
![cover](./cover.png)
2+
3+
# Day 44 - Queue from Stacks
4+
5+
Implement the Queue data structure using Stacks
6+
7+
### Hint
8+
9+
Use 2 stacks
10+
11+
- enqueue operation same as push in a single stack
12+
- For dequeue, pop all the elements of first stack into second and apply pop once in second queue
13+
- for peek, again, pop all the elements of first stack into second and apply peek once in second queue
14+
15+
Don't forget to restore the state of first stack after the dequeue and peek operations
16+
17+
## Solution
18+
19+
## JavaScript Implementation
20+
21+
### [Solution](./JavaScript/queueFromStack.js)
22+
23+
```js
24+
class Queue {
25+
constructor(size) {
26+
this.first = new Stack();
27+
this.second = new Stack();
28+
}
29+
30+
enqueue(element) {
31+
this.first.push(element);
32+
}
33+
34+
dequeue() {
35+
// Empty first stack to second
36+
while (this.first.peek()) {
37+
this.second.push(this.first.pop());
38+
}
39+
40+
const element = this.second.pop();
41+
42+
// Restore first stack
43+
while (this.second.peek()) {
44+
this.first.push(this.second.pop());
45+
}
46+
47+
return element;
48+
}
49+
50+
peek() {
51+
while (this.first.peek()) {
52+
this.second.push(this.first.pop());
53+
}
54+
55+
const element = this.second.peek();
56+
57+
while (this.second.peek()) {
58+
this.first.push(this.second.pop());
59+
}
60+
61+
return element;
62+
}
63+
}
64+
```
65+
66+
[Click Here](./JavaScript/queueFromStack.js) for complete solution

day44/cover.png

138 KB
Loading

day44/ques.png

944 KB
Loading

0 commit comments

Comments
 (0)