Created
December 3, 2018 12:37
-
-
Save rousan/86c06ca567dcf2caf60fab01d0ccf4f1 to your computer and use it in GitHub Desktop.
Stack using Queue
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._queue = new Queue(); | |
this._size = 0; | |
} | |
push(val) { | |
this._queue.enqueue(val) | |
} | |
pop() { | |
if (this._size == 0) { | |
return null; | |
} | |
for (let i = 0; i < this._size - 1; i++) { | |
this._queue.enqueue(this._queue.dequeue()) | |
} | |
this._size--; | |
return this._queue.dequeue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment