var MyQueue = function () { this.inStack = []; this.outStack = []; }; MyQueue.prototype.push = function (x) { this.inStack.push(x); }; MyQueue.prototype.pop = function () { this._transfer(); return this.outStack.pop(); }; MyQueue.prototype.peek = function () { this._transfer(); return this.outStack[this.outStack.length - 1]; }; MyQueue.prototype.empty = function () { return !this.inStack.length && !this.outStack.length; }; MyQueue.prototype._transfer = function () { if (!this.outStack.length) while (this.inStack.length) this.outStack.push(this.inStack.pop()); };
|