Last active
August 15, 2017 00:50
-
-
Save evandocarmo/c4d31ddfb8cd31d2452e06d00f4328c4 to your computer and use it in GitHub Desktop.
Implementation of a Queue class using two stacks as a challenge for HackerRank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Queue { | |
constructor() { | |
this.inStack = []; | |
this.outStack = []; | |
} | |
enqueue(element) { | |
this.inStack.push(element); | |
} | |
dequeue() { | |
if (!this.outStack.length) | |
while (this.inStack.length) | |
this.outStack.push(this.inStack.pop()); | |
this.outStack.pop(); | |
} | |
print() { | |
if (this.outStack.length) | |
console.log(this.outStack[this.outStack.length - 1]); | |
else if (this.inStack.length) | |
console.log(this.inStack[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment