js 实现数据结构 -- 队列(Queue)
原文:
概念:
与栈相反,队列是一种遵循先进先出 (FIFO / First In First Out) 原则的一组有序的项;队列在尾部添加新元素,并从头部移除元素。最新添加的元素必须排在队列的末尾。在现实中,最常见的例子就是排队,吃饭排队、银行业务排队、公车的前门上后门下机制...,前面的人优先完成自己的事务,完成之后,下一个人才能继续。常见的应用形式是视频网站的缓冲以及打印队列。
基础队列:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | class Queue { constructor(items) { this .items = items || []; } // 入列 enqueue(ele) { this .items.push(ele); } // 出列 dequeue() { this .items.shift(); } // front() { return this .items[0]; } clear() { this .items = []; } get size() { return this .items.length; } get isEmpty() { return ! this .items.length; } print() { console.log( this .items.toString()); } } let queue = new Queue() console.log(queue.isEmpty) // true queue.enqueue( 'John' ) queue.enqueue( 'Jack' ) queue.enqueue( 'Camila' ) console.log(queue.size) // 3 console.log(queue.isEmpty) // false queue.dequeue() queue.dequeue() queue.print() // 'Camila' |
优先队列:
如医院的急诊,虽然都有专门的急诊科,但是就理解而言严重程度高的可以先就诊。优先队列可以根据优先级进行排列。
实现思路也简单,普通队列直接 push 元素,那么优先队列就是插入包含优先级的对象,插入前先进行优先级验证,在相应位置插入元素。
下例实现根据优先级插入数据,出列操作同普通队列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | class PriorityQueue { constructor() { this .items = []; } get isEmpty() { return ! this .items.length; } get size() { return this .items.length; } enqueue(ele, priority) { let queueEle = { ele, priority }; if ( this .isEmpty) { this .items.push(queueEle); } else { let preIndex = this .items.findIndex(item => queueEle.priority < item.priority); if (preIndex > -1) { this .items.splice(preIndex, 0, queueEle); } else { this .items.push(queueEle); } } } dequeue() { return this .items.shift(); } front() { return this .items[0]; } clear() { this .items = []; } print() { console.log( this .items) } } let priorityQueue = new PriorityQueue() priorityQueue.enqueue( 'John' , 2) priorityQueue.enqueue( 'Jack' , 1) priorityQueue.enqueue( 'Camila' , 1) priorityQueue.enqueue( 'Surmon' , 3) priorityQueue.enqueue( 'skyRover' , 2) priorityQueue.enqueue( '司马萌' , 1) priorityQueue.print() console.log(priorityQueue.isEmpty, priorityQueue.size) // false 6 |
循环队列:
循环队列在概念上就是将队尾和队首闭环。
为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列(Circular Queue)。这种循环队列可以以单链表的方式来在实际编程应用中来实现。
js 实现的循环队列:(基于普通队列)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | class Queue { constructor(items) { this .items = items || []; } // 入列 enqueue(ele) { this .items.push(ele); } // 出列 dequeue() { this .items.shift(); } // front() { return this .items[0]; } clear() { this .items = []; } get size() { return this .items.length; } get isEmpty() { return ! this .items.length; } print() { console.log( this .items.toString()); } } class LoopQueue extends Queue { constructor(items) { super (items) } getIndex(index) { let length = this .items.length; return index > length ? index % length : index; } findIndex(index) { return ! this .isEmpty ? this .items[ this .getIndex(index)] : null ; } } const loopQueue = new LoopQueue([ 'Surmon' ]) loopQueue.enqueue( 'SkyRover' ) loopQueue.enqueue( 'Even' ) loopQueue.enqueue( 'Alice' ) console.log(loopQueue.size, loopQueue.isEmpty) // 4 false console.log(loopQueue.find(26)) // 'Evan' console.log(loopQueue.find(87651)) // 'Alice' |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码