xiqe / code-train

前端算法
0 stars 0 forks source link

用栈实现队列 #206

Open xiqe opened 5 years ago

xiqe commented 5 years ago

使用栈实现队列的下列操作:

示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);  
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 false

说明:

reply

class MyQueue{
    constructor(){
        this.list = new Array();
    }

    push(val){
        this.list.push(val)
    }

    pop(){
        return this.list.shift()
    }

    peek(){
        return this.list[0]
    }

    empty(){
        return this.list.length == 0
    }
}