Skip to content

Commit

Permalink
Add day 44
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Feb 19, 2019
1 parent 3122651 commit e8a9631
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -71,6 +71,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
| [Day 41](./day41) | [Implement Queue Data Structure](./day41) | [http://codetoexpress.tech/dc/day41/](http://codetoexpress.tech/dc/day41/) | **Beginner** |
| [Day 42](./day42) | [Alternate Queue Combination](./day42) | [http://codetoexpress.tech/dc/day42/](http://codetoexpress.tech/dc/day42/) | **intermediate** |
| [Day 43](./day43) | [Queue Reversal](./day43) | [http://codetoexpress.tech/dc/day43/](http://codetoexpress.tech/dc/day43/) | **intermediate** |
| [Day 44](./day44) | [Queue from Stacks](./day44) | [http://codetoexpress.tech/dc/day44/](http://codetoexpress.tech/dc/day44/) | **intermediate** |

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

Expand Down
79 changes: 79 additions & 0 deletions day44/JavaScript/queueFromStack.js
@@ -0,0 +1,79 @@
/**
* Queue from stack
*/

// Class declaration for queue data structure
class Queue {
constructor(size) {
this.first = new Stack();
this.second = new Stack();
}

enqueue(element) {
this.first.push(element);
}

dequeue() {
// Empty first stack to second
while (this.first.peek()) {
this.second.push(this.first.pop());
}

const element = this.second.pop();

// Restore first stack
while (this.second.peek()) {
this.first.push(this.second.pop());
}

return element;
}

peek() {
while (this.first.peek()) {
this.second.push(this.first.pop());
}

const element = this.second.peek();

while (this.second.peek()) {
this.first.push(this.second.pop());
}

return element;
}
}

// Class declaration for stack data structure
class Stack {
constructor() {
this.data = [];
}

push(record) {
this.data.push(record);
}

pop() {
return this.data.pop();
}

peek() {
return this.data[this.data.length - 1];
}
}

const q = new Queue();

q.enqueue(1);
console.log(q.dequeue());
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
console.log(q.dequeue());
console.log(q.dequeue());
console.log(q.dequeue());
q.enqueue(1);
q.enqueue(2);
console.log(q.peek());
console.log(q.peek());
66 changes: 66 additions & 0 deletions day44/README.md
@@ -0,0 +1,66 @@
![cover](./cover.png)

# Day 44 - Queue from Stacks

Implement the Queue data structure using Stacks

### Hint

Use 2 stacks

- enqueue operation same as push in a single stack
- For dequeue, pop all the elements of first stack into second and apply pop once in second queue
- for peek, again, pop all the elements of first stack into second and apply peek once in second queue

Don't forget to restore the state of first stack after the dequeue and peek operations

## Solution

## JavaScript Implementation

### [Solution](./JavaScript/queueFromStack.js)

```js
class Queue {
constructor(size) {
this.first = new Stack();
this.second = new Stack();
}

enqueue(element) {
this.first.push(element);
}

dequeue() {
// Empty first stack to second
while (this.first.peek()) {
this.second.push(this.first.pop());
}

const element = this.second.pop();

// Restore first stack
while (this.second.peek()) {
this.first.push(this.second.pop());
}

return element;
}

peek() {
while (this.first.peek()) {
this.second.push(this.first.pop());
}

const element = this.second.peek();

while (this.second.peek()) {
this.first.push(this.second.pop());
}

return element;
}
}
```

[Click Here](./JavaScript/queueFromStack.js) for complete solution
Binary file added day44/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day44/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e8a9631

Please sign in to comment.