用两个栈实现一个队列

#code

class Queue {
    #stack1 = []
    #stack2 = []
    add(value){
        this.#stack1.push(value)
        return this.#stack1.length
    }
    delete(){
        while(this.#stack1.length){
            this.#stack2.push(this.#stack1.pop())
        }
        const popValue = this.#stack2.pop()
        while(this.#stack2.length){
            this.#stack1.push(this.#stack2.pop())
        }
        return popValue
    }
    get length(){
        return this.#stack1.length
    }
}

  

posted @ 2023-01-26 00:53  671_MrSix  阅读(8)  评论(0编辑  收藏  举报