Skip to content

Commit 7533c68

Browse files
committed
Add day42
1 parent fe0c654 commit 7533c68

File tree

9 files changed

+197
-0
lines changed

9 files changed

+197
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
6969
| [Day 39](./day39) | [Maximum Element and Reverse Stack](./day39) | [http://codetoexpress.tech/dc/day39/](http://codetoexpress.tech/dc/day39/) | **Intermediate** |
7070
| [Day 40](./day40) | [Prefix, Infix, Postfix Conversion](./day40) | [http://codetoexpress.tech/dc/day40/](http://codetoexpress.tech/dc/day40/) | **Intermediate** |
7171
| [Day 41](./day41) | [Implement Queue Data Structure](./day41) | [http://codetoexpress.tech/dc/day41/](http://codetoexpress.tech/dc/day41/) | **Beginner** |
72+
| [Day 42](./day42) | [Alternate Queue Combination](./day42) | [http://codetoexpress.tech/dc/day42/](http://codetoexpress.tech/dc/day42/) | **intermediate** |
7273

7374
## [More Problems](./BONUS/README.md)
7475

day40/ques.png

975 KB
Loading

day41/ques.png

1.16 MB
Loading

day42/Daily Codes.png

142 KB
Loading

day42/JavaScript/alternateQueue.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Alternate Queue Combination
3+
* @author MadhavBahlMD
4+
* @date 15/02/2019
5+
*/
6+
7+
class Queue {
8+
constructor (size) {
9+
this.capacty = size;
10+
this.data = [];
11+
this.frontIndex = 0;
12+
this.rearIndex = 0;
13+
}
14+
15+
front () {
16+
return data[this.frontIndex];
17+
}
18+
19+
rear () {
20+
return data[this.rearIndex];
21+
}
22+
23+
enqueue (element) {
24+
if (this.rearIndex >= this.capacty) {
25+
console.log ("Overflow!");
26+
return -1;
27+
}
28+
this.data.unshift (element);
29+
// console.log (element + ' added to the queue');
30+
this.rearIndex++;
31+
}
32+
33+
dequeue (element) {
34+
if (this.rearIndex === 0) {
35+
console.log ("Underflow!");
36+
return -1;
37+
}
38+
// console.log (this.data[this.rearIndex -1] + ' has been removed from the queue');
39+
this.rearIndex--;
40+
return this.data.pop ();
41+
}
42+
43+
displayQueue () {
44+
let toBeDisplayed = '-> ';
45+
for (let element of this.data) {
46+
toBeDisplayed += element + ' -> ';
47+
}
48+
console.log (toBeDisplayed);
49+
}
50+
}
51+
52+
const combine = (q1, q2) => {
53+
let toBeAdded;
54+
const combinedQueue = new Queue (q1.rearIndex + q2.rearIndex);
55+
while (!(q1.rearIndex === 0 || q2.rearIndex === 0)) {
56+
toBeAdded = q1.dequeue();
57+
combinedQueue.enqueue (toBeAdded);
58+
toBeAdded = q2.dequeue();
59+
combinedQueue.enqueue (toBeAdded);
60+
}
61+
console.log ("\n/* ===== Displaying Combined Queue ===== */");
62+
combinedQueue.displayQueue();
63+
}
64+
65+
const firstQ = new Queue (4);
66+
67+
firstQ.enqueue (1);
68+
firstQ.enqueue (2);
69+
firstQ.enqueue (3);
70+
firstQ.enqueue (4);
71+
72+
const secondQ = new Queue (4);
73+
74+
secondQ.enqueue (5);
75+
secondQ.enqueue (6);
76+
secondQ.enqueue (7);
77+
secondQ.enqueue (8);
78+
79+
console.log ("\n/* ===== Displaying Queue 1 ===== */");
80+
firstQ.displayQueue();
81+
console.log ("\n/* ===== Displaying Queue 2 ===== */");
82+
secondQ.displayQueue();
83+
84+
combine (firstQ, secondQ);

day42/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
![cover](./cover.png)
2+
3+
# Day 42 - Alternate Queue Combination
4+
5+
Given 2 queues, write a function to combine those 2 queues into a single queue such that the elements in the combined queue are from first queue and second queue alternatively
6+
7+
If the lengths of queues are not equal, stop after all elements of the smaller queue are addded to the combined queue
8+
9+
![process](./proc.png)
10+
11+
## Example:
12+
13+
```
14+
q1 -> 4 -> 3 -> 2 -> 1 ->
15+
q2 -> 8 -> 7 -> 6 -> 5 ->
16+
17+
combined queue:
18+
-> 8 -> 4 -> 7 -> 3 -> 6 -> 2 -> 5 -> 1 ->
19+
```
20+
21+
![ques](./ques.png)
22+
23+
## Solution
24+
25+
## JavaScript Implementation
26+
27+
### [Solution](./JavaScript/queue.js)
28+
29+
```js
30+
class Queue {
31+
constructor (size) {
32+
this.capacty = size;
33+
this.data = [];
34+
this.frontIndex = 0;
35+
this.rearIndex = 0;
36+
}
37+
38+
front () {
39+
return data[this.frontIndex];
40+
}
41+
42+
rear () {
43+
return data[this.rearIndex];
44+
}
45+
46+
enqueue (element) {
47+
if (this.rearIndex >= this.capacty) {
48+
console.log ("Overflow!");
49+
return -1;
50+
}
51+
this.data.unshift (element);
52+
// console.log (element + ' added to the queue');
53+
this.rearIndex++;
54+
}
55+
56+
dequeue (element) {
57+
if (this.rearIndex === 0) {
58+
console.log ("Underflow!");
59+
return -1;
60+
}
61+
// console.log (this.data[this.rearIndex -1] + ' has been removed from the queue');
62+
this.rearIndex--;
63+
return this.data.pop ();
64+
}
65+
66+
displayQueue () {
67+
let toBeDisplayed = '-> ';
68+
for (let element of this.data) {
69+
toBeDisplayed += element + ' -> ';
70+
}
71+
console.log (toBeDisplayed);
72+
}
73+
}
74+
75+
const combine = (q1, q2) => {
76+
let toBeAdded;
77+
const combinedQueue = new Queue (q1.rearIndex + q2.rearIndex);
78+
while (!(q1.rearIndex === 0 || q2.rearIndex === 0)) {
79+
toBeAdded = q1.dequeue();
80+
combinedQueue.enqueue (toBeAdded);
81+
toBeAdded = q2.dequeue();
82+
combinedQueue.enqueue (toBeAdded);
83+
}
84+
console.log ("\n/* ===== Displaying Combined Queue ===== */");
85+
combinedQueue.displayQueue();
86+
}
87+
88+
const firstQ = new Queue (4);
89+
90+
firstQ.enqueue (1);
91+
firstQ.enqueue (2);
92+
firstQ.enqueue (3);
93+
firstQ.enqueue (4);
94+
95+
const secondQ = new Queue (4);
96+
97+
secondQ.enqueue (5);
98+
secondQ.enqueue (6);
99+
secondQ.enqueue (7);
100+
secondQ.enqueue (8);
101+
102+
console.log ("\n/* ===== Displaying Queue 1 ===== */");
103+
firstQ.displayQueue();
104+
console.log ("\n/* ===== Displaying Queue 2 ===== */");
105+
secondQ.displayQueue();
106+
107+
combine (firstQ, secondQ);
108+
```
109+
110+
#### Output
111+
112+
![output](./output.png)

day42/output.PNG

6.43 KB
Loading

day42/proc.png

68.7 KB
Loading

day42/ques.png

1.03 MB
Loading

0 commit comments

Comments
 (0)