Any reason to not use Array.prototype.shift()
?
I think is more clear and should be faster
class Queue { constructor() { this._stack = []; // We will be using this array as a stack - only // its push and pop operations will ever be called } enqueue(data) { this._stack.push(data); } dequeue() { return this._stack.shift(); } }
- class Queue {
- constructor() {
- this._stack = []; // We will be using this array as a stack - only
- // its push and pop operations will ever be called
- }
- enqueue(data) {
- this._stack.push(data);
- }
- dequeue() {
if (this._stack.length === 1)return this._stack.pop();else {var tmp = this._stack.pop(), result = this.dequeue();this.enqueue(tmp);return result;}- return this._stack.shift();
- }
- }