Created
April 23, 2019 08:23
-
-
Save 4skinSkywalker/b3fa985ca0672483f7f58facec4cbe6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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