Skip to content

Instantly share code, notes, and snippets.

@evandocarmo
Last active August 15, 2017 00:50
Show Gist options
  • Save evandocarmo/c4d31ddfb8cd31d2452e06d00f4328c4 to your computer and use it in GitHub Desktop.
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
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