Skip to content

Commit

Permalink
Add day 43
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Feb 18, 2019
1 parent 382c069 commit 3122651
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
16 changes: 13 additions & 3 deletions day43/JavaScript/queueReversal.js
Expand Up @@ -21,7 +21,7 @@ class Queue {
}

rear () {
return data[this.rearIndex];
return data[this.rearIndex - 1];
}

enqueue (element) {
Expand Down Expand Up @@ -76,7 +76,6 @@ class Stack {
// push method to add a record to the stack
push (record) {
if (!this.isFull()) {
console.log (`Pushing ${record} to the stack!`);
this.myStack.push (record);
} else {
console.log ('Sorry! The Stack is full!');
Expand All @@ -86,7 +85,6 @@ class Stack {
// pop method to remove an element from the stack
pop () {
if (!this.isEmpty()) {
console.log (`Popped element is: ${this.myStack[this.myStack.length-1]}`);
return this.myStack.pop ();
} else {
console.log ('Sorry! The Stack is empty');
Expand All @@ -100,7 +98,18 @@ class Stack {
}

const reverse = (myQueue) => {
const stack = new Stack (10);
let len = myQueue.rearIndex;

for (let i=len; i>0; i--) {
let currentElement = myQueue.dequeue();
stack.push (currentElement);
}

for (let i=0; i<len; i++) {
let currentElement = stack.pop();
myQueue.enqueue(currentElement);
}
};

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

reverse (myQueue);

console.log ("\n/* ===== Displaying Final Queue ===== */");
myQueue.displayQueue();
19 changes: 17 additions & 2 deletions day43/README.md
Expand Up @@ -27,5 +27,20 @@ Output:
### [Solution](./JavaScript/queueReversal.js)

```js
To Be Added
```
const reverse = (myQueue) => {
const stack = new Stack (10);
let len = myQueue.rearIndex;

for (let i=len; i>0; i--) {
let currentElement = myQueue.dequeue();
stack.push (currentElement);
}

for (let i=0; i<len; i++) {
let currentElement = stack.pop();
myQueue.enqueue(currentElement);
}
};
```

[Click Here](./JavaScript/queueReversal.js) for complete solution

0 comments on commit 3122651

Please sign in to comment.