链表和数组哪个实现队列更快
链表 -> 队列
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()
}
}
以自己现在的努力程度,还没有资格和别人拼天赋

浙公网安备 33010602011771号