Skip to content

Commit 3122651

Browse files
committed
Add day 43
1 parent 382c069 commit 3122651

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

day43/JavaScript/queueReversal.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Queue {
2121
}
2222

2323
rear () {
24-
return data[this.rearIndex];
24+
return data[this.rearIndex - 1];
2525
}
2626

2727
enqueue (element) {
@@ -76,7 +76,6 @@ class Stack {
7676
// push method to add a record to the stack
7777
push (record) {
7878
if (!this.isFull()) {
79-
console.log (`Pushing ${record} to the stack!`);
8079
this.myStack.push (record);
8180
} else {
8281
console.log ('Sorry! The Stack is full!');
@@ -86,7 +85,6 @@ class Stack {
8685
// pop method to remove an element from the stack
8786
pop () {
8887
if (!this.isEmpty()) {
89-
console.log (`Popped element is: ${this.myStack[this.myStack.length-1]}`);
9088
return this.myStack.pop ();
9189
} else {
9290
console.log ('Sorry! The Stack is empty');
@@ -100,7 +98,18 @@ class Stack {
10098
}
10199

102100
const reverse = (myQueue) => {
101+
const stack = new Stack (10);
102+
let len = myQueue.rearIndex;
103103

104+
for (let i=len; i>0; i--) {
105+
let currentElement = myQueue.dequeue();
106+
stack.push (currentElement);
107+
}
108+
109+
for (let i=0; i<len; i++) {
110+
let currentElement = stack.pop();
111+
myQueue.enqueue(currentElement);
112+
}
104113
};
105114

106115
const myQueue = new Queue (4);
@@ -113,6 +122,7 @@ myQueue.enqueue (4);
113122
console.log ("\n/* ===== Displaying Initial Queue ===== */");
114123
myQueue.displayQueue();
115124

125+
reverse (myQueue);
116126

117127
console.log ("\n/* ===== Displaying Final Queue ===== */");
118128
myQueue.displayQueue();

day43/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,20 @@ Output:
2727
### [Solution](./JavaScript/queueReversal.js)
2828

2929
```js
30-
To Be Added
31-
```
30+
const reverse = (myQueue) => {
31+
const stack = new Stack (10);
32+
let len = myQueue.rearIndex;
33+
34+
for (let i=len; i>0; i--) {
35+
let currentElement = myQueue.dequeue();
36+
stack.push (currentElement);
37+
}
38+
39+
for (let i=0; i<len; i++) {
40+
let currentElement = stack.pop();
41+
myQueue.enqueue(currentElement);
42+
}
43+
};
44+
```
45+
46+
[Click Here](./JavaScript/queueReversal.js) for complete solution

0 commit comments

Comments
 (0)