首页 > 其他分享 >链表和数组哪个实现队列更快

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

时间:2023-01-26 18:22:06浏览次数:33  
标签:queue cur 队列 value next 链表 数组

链表 -> 队列

class QueueByLinkList{
    constructor(){
        this.queue = null
    }
    add(value){
        if(!this.queue){
            this.queue = {
                value,
                next: null
            }
            return
        }
        let cur = this.queue
        while(cur.next){
            cur = cur.next
        }
        cur.next = {
            value,
            next:null
        }
    }
    shift(){
        const value = this.queue.value
        this.queue = this.queue.next
        return value
    }
}

  数组 -> 队列

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

  

标签:queue,cur,队列,value,next,链表,数组
From: https://www.cnblogs.com/zhenjianyu/p/17068021.html

相关文章

  • 反转单向链表
    数组生成单向链表constcreateLinkList=(arr=[1,2,3,4,5,6])=>{constlength=arr.length;letcurNode={value:arr[arr.length-1],......
  • c++ 数组
              ......
  • 【Java】阻塞队列
    【Java】阻塞队列什么是阻塞队列?阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这2个附加的操作支持阻塞的插入和移除方法。支持阻塞的插入方法:当队列满时,队列会阻塞......
  • 【奇妙的数据结构世界】用图像和代码对链表的使用进行透彻学习 | C++
    第九章链表:::hljs-center目录第九章链表●前言●一、链表是什么?1.简要介绍2.具体情况●二、链表操作的关键代码段1.类型定义2.常用操作●总结:::......
  • js 数组方法小计
    Array使用方法小计用于检测数组所有元素是否都符合指定条件everyevery()方法使用指定函数检测数组中的所有元素:如果数组中检测到有一个元素不满足,则整个表达式返回......
  • 用两个栈实现一个队列
    #codeclassQueue{#stack1=[]#stack2=[]add(value){this.#stack1.push(value)returnthis.#stack1.length}delete(){......
  • POJ--3253 Fence Repair(贪心/优先队列)
    记录23:572023-1-25http://poj.org/problem?id=3253reference:《挑战程序设计竞赛(第2版)》2.2.4p47DescriptionFarmerJohnwantstorepairasmalllengthofth......
  • POJ--2431 Expedition(优先队列)
    记录0:172023-1-26http://poj.org/problem?id=2431reference:《挑战程序设计竞赛(第2版)》2.2.4p77DescriptionAgroupofcowsgrabbedatruckandventuredona......
  • JavaScript:判断数组对象值是否相同的函数声明
    varobj1={name:"w",};varobj2={name:"w",};functionisObjectValueEqual(a,b){//判断两个对......
  • 数组旋转k步
    时间复杂度O(n^2)空间复杂度O(1)constrorateKstep=(arr=[1,2,3,4,5,6,7],step=3)=>{constlength=arr.lengthfor(leti=0;i<3;i++){......