1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| var MyQueue = function () { this.stackIn = []; this.stackOut = []; };
MyQueue.prototype.push = function (x) { this.stackIn.push(x); };
MyQueue.prototype.pop = function () { const size = this.stackOut.length; if (size > 0) return this.stackOut.pop();
while (this.stackIn.length > 0) { this.stackOut.push(this.stackIn.pop()); } return this.stackOut.pop(); };
MyQueue.prototype.peek = function () { const x = this.pop(); this.stackOut.push(x); return x; };
MyQueue.prototype.empty = function () { return this.stackIn.length === 0 && this.stackOut.length === 0; };
|