链表和数组哪个实现队列更快

  链表 -> 队列

class QueueByLinkList{
    #head = null
    #tail = null
    #len = 0
    add(value){
        const newNode = {
            value,
            next: null
        }
        if(!this.#head){
            this.#head = newNode
        }
        const tailNode = this.#tail
        if(tailNode){
            tailNode.next = newNode
        }
        this.#tail = newNode
        this.#len++
    }
    delete(){
        const headNode = this.#head
        if(!headNode){
            return null
        }
        this.#len--
        this.#head = headNode.next
        return headNode.value
    }
    get length (){
        return this.#len
    }
}

  数组 -> 队列

class QueueByArray {
    constructor(){
        this.queue = []
    }
    push(value){
        this.queue.push(value)
    }
    shift(){
        this.queue.shift()
    }
}

  

posted @ 2023-01-26 18:14  671_MrSix  阅读(23)  评论(0编辑  收藏  举报