Skip to content

Commit b1a0cef

Browse files
committed
Add day 43
1 parent 795b67e commit b1a0cef

File tree

6 files changed

+151
-1
lines changed

6 files changed

+151
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
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** |
7272
| [Day 42](./day42) | [Alternate Queue Combination](./day42) | [http://codetoexpress.tech/dc/day42/](http://codetoexpress.tech/dc/day42/) | **intermediate** |
73+
| [Day 43](./day43) | [Queue Reversal](./day43) | [http://codetoexpress.tech/dc/day43/](http://codetoexpress.tech/dc/day43/) | **intermediate** |
7374

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

day42/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ combined queue:
2424

2525
## JavaScript Implementation
2626

27-
### [Solution](./JavaScript/queue.js)
27+
### [Solution](./JavaScript/alternateQueue.js)
2828

2929
```js
3030
class Queue {

day43/JavaScript/queueReversal.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Queue reversal
3+
* @author MadhavBahlMD
4+
* @date 15/02/2019
5+
* Method
6+
* - Dequeue the elements from queue and keep pushing them in stack
7+
* - Pop the elements from stack and enqueue them in the original queue
8+
*/
9+
10+
// Class declaration for queue data structure
11+
class Queue {
12+
constructor (size) {
13+
this.capacty = size;
14+
this.data = [];
15+
this.frontIndex = 0;
16+
this.rearIndex = 0;
17+
}
18+
19+
front () {
20+
return data[this.frontIndex];
21+
}
22+
23+
rear () {
24+
return data[this.rearIndex];
25+
}
26+
27+
enqueue (element) {
28+
if (this.rearIndex >= this.capacty) {
29+
console.log ("Overflow!");
30+
return -1;
31+
}
32+
this.data.unshift (element);
33+
// console.log (element + ' added to the queue');
34+
this.rearIndex++;
35+
}
36+
37+
dequeue (element) {
38+
if (this.rearIndex === 0) {
39+
console.log ("Underflow!");
40+
return -1;
41+
}
42+
// console.log (this.data[this.rearIndex -1] + ' has been removed from the queue');
43+
this.rearIndex--;
44+
return this.data.pop ();
45+
}
46+
47+
displayQueue () {
48+
let toBeDisplayed = '-> ';
49+
for (let element of this.data) {
50+
toBeDisplayed += element + ' -> ';
51+
}
52+
console.log (toBeDisplayed);
53+
}
54+
}
55+
56+
// Class declaration for stack data structure
57+
class Stack {
58+
// constructor to initialize the stack and it's capacity
59+
constructor (limit) {
60+
this.myStack = [];
61+
this.top = limit;
62+
}
63+
64+
// isEmpty method to check whether the stack is empty
65+
isEmpty () {
66+
if (this.myStack.length === 0) return true;
67+
else return false;
68+
}
69+
70+
// isFull method to check whether the stack is full
71+
isFull () {
72+
if (this.myStack.length === this.top) return true;
73+
else return false;
74+
}
75+
76+
// push method to add a record to the stack
77+
push (record) {
78+
if (!this.isFull()) {
79+
console.log (`Pushing ${record} to the stack!`);
80+
this.myStack.push (record);
81+
} else {
82+
console.log ('Sorry! The Stack is full!');
83+
}
84+
}
85+
86+
// pop method to remove an element from the stack
87+
pop () {
88+
if (!this.isEmpty()) {
89+
console.log (`Popped element is: ${this.myStack[this.myStack.length-1]}`);
90+
return this.myStack.pop ();
91+
} else {
92+
console.log ('Sorry! The Stack is empty');
93+
}
94+
}
95+
96+
// peek method to view the top element
97+
peek () {
98+
console.log (`Current element is: ${this.myStack[this.myStack.length - 1]}`);
99+
}
100+
}
101+
102+
const reverse = (myQueue) => {
103+
104+
};
105+
106+
const myQueue = new Queue (4);
107+
108+
myQueue.enqueue (1);
109+
myQueue.enqueue (2);
110+
myQueue.enqueue (3);
111+
myQueue.enqueue (4);
112+
113+
console.log ("\n/* ===== Displaying Initial Queue ===== */");
114+
myQueue.displayQueue();
115+
116+
117+
console.log ("\n/* ===== Displaying Final Queue ===== */");
118+
myQueue.displayQueue();

day43/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
![cover](./cover.png)
2+
3+
# Day 43 - Queue Reversal
4+
5+
Write a program to reverse the given queue.
6+
7+
#### Hint
8+
9+
Use stack as an intermediate data structure
10+
11+
## Example:
12+
13+
```
14+
Input:
15+
q1 -> 4 -> 3 -> 2 -> 1 ->
16+
17+
Output:
18+
q1 -> 1 -> 2 -> 3 -> 4 ->
19+
```
20+
21+
![ques](./ques.png)
22+
23+
## Solution
24+
25+
## JavaScript Implementation
26+
27+
### [Solution](./JavaScript/queueReversal.js)
28+
29+
```js
30+
To Be Added
31+
```

day43/cover.png

137 KB
Loading

day43/ques.png

944 KB
Loading

0 commit comments

Comments
 (0)