1. var MyQueue = function() {
    2. this.inStack = [];
    3. this.outStack = [];
    4. };
    5. MyQueue.prototype.push = function(x) {
    6. this.inStack.push(x);
    7. };
    8. MyQueue.prototype.pop = function() {
    9. if (!this.outStack.length) {
    10. this.in2out();
    11. }
    12. return this.outStack.pop();
    13. };
    14. MyQueue.prototype.peek = function() {
    15. if (!this.outStack.length) {
    16. this.in2out();
    17. }
    18. return this.outStack[this.outStack.length - 1];
    19. };
    20. MyQueue.prototype.empty = function() {
    21. return this.outStack.length === 0 && this.inStack.length === 0;
    22. };
    23. MyQueue.prototype.in2out = function() {
    24. while (this.inStack.length) {
    25. this.outStack.push(this.inStack.pop());
    26. }
    27. }