用链表实现队列

问题:链表和数组,哪个队列更快?

分析:

  • 队列是先进后出
  • 数组是连续存储的,push很快,shift 很慢  
  • 链表是非连续存储,add 和 delete 都很快
  • 结论:链表实现队列更快

链表实现队列

  • 单向链表,要同时记录 head 和 tail
  • 要从tail 入队,从head 出队
  • length要实时记录,不可遍历链表获取

代码实现:

复制代码
/**
 * @description 链表实现队列
 * @author ykk
 */
interface ILinkListNode {
    value: number
    next: ILinkListNode | null
}
export class QueueWithLink {
    private head:ILinkListNode | null = null
    private tail:ILinkListNode | null = null
    private len: number = 0

    /**
     * 入队
     */
    add(n:number){
        const newNode:ILinkListNode = {
            value: n,
            next: null
        }
        //处理 head
        if(this.head == null){
            this.head = newNode
        }
        //处理 tail
        const tailNode = this.tail 
        if(tailNode){
            tailNode.next = newNode
        }
        this.tail = newNode
        this.len++
    }
    /**
     * 出队,在 head位置
     */
    delete():number | null{
        if(this.head == null  || this.len === 0){
            return null
        }
        //取值
        const value = this.head.value
        
        //修改头节点,指向下一个
        this.head = this.head.next

        // 记录长度
        this.len--

        return value
    }
    get length(): number{
        return this.len
    }
}
复制代码

 

测试用例:

复制代码
/**
 * @description 链表实现队列
 * @author ykk
 */
import {QueueWithLink} from './queue-with-list'
describe('链表实现队列测试', () =>{
    it('add delete',()=>{
        const q = new QueueWithLink()
        expect(q.length).toBe(0)
        q.add(100)
        q.add(200)
        q.add(300)
        expect(q.length).toBe(3)
    })
    it('length',()=>{
        const q = new QueueWithLink()
        expect(q.delete()).toBeNull()
        q.add(100)
        q.add(200)
        q.add(300)

        expect(q.delete()).toBe(100)
        expect(q.delete()).toBe(200)
    })
})
复制代码

 

性能测试:

 数组实现队列 和 链表实现队列 删除元素的性能测试,代码如下

复制代码
//性能测试:
const p1 = new QueueWithLink()
for(let i = 0; i < 10 * 10000; i++){
    p1.add(i)
}
console.time('p1')
for(let i = 0; i < 10 * 10000; i++){
    p1.delete()
}
console.timeEnd('p1')

const q2 = []
for(let i = 0; i < 10 * 10000; i++){
    q2.push(i)
}
console.time('q2')
for(let i = 0; i < 10 * 10000; i++){
    q2.shift()
}
console.timeEnd('q2')
复制代码

 

执行时间对比:

链表删除的时间比 数组删除数据的时间要快5千多ms。 数字shift 执行时间非常慢

 

posted @   yangkangkang  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2023-02-24 css相关问题总结
点击右上角即可分享
微信分享提示