Skip to content

Instantly share code, notes, and snippets.

@4skinSkywalker
Created April 23, 2019 08:23
Show Gist options
  • Save 4skinSkywalker/b3fa985ca0672483f7f58facec4cbe6c to your computer and use it in GitHub Desktop.
Save 4skinSkywalker/b3fa985ca0672483f7f58facec4cbe6c to your computer and use it in GitHub Desktop.
class Stack {
constructor() {
this.array = []
}
push(value) {
this.array.push(value)
}
pop() {
return this.array.pop()
}
isEmpty() {
return !this.array.length
}
}
class Queue {
constructor() {
this.s1 = new Stack()
this.s2 = new Stack()
}
enqueue(value) {
this.s1.push(value)
}
dequeue() {
if (!this.s2.isEmpty) {
return this.s2.pop()
}
while (!this.s1.isEmpty()) {
this.s2.push(this.s1.pop())
}
return this.s2.pop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment