Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement js priority queue

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to achieve the js priority queue, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.

In the priority queue, the addition or deletion of elements is based on priority.

There are two ways to implement priority queues:

1. Add it first and dequeue normally. On the basis of implementing the queue, change the element added to the queue from normal data to an object (array) type, which contains the value and priority of the element to be added to the queue.

2. Add normally and dequeue first.

Example

Function PriorityQueue () {/ / initialize queue (implemented using arrays) var items = [] / / because priority exists So the inserted queue should have a priority attribute function queueEle (ele, priority) {this.ele = ele this.priority = priority} / / queue this.enqueue = function (ele, priority) {let element = new queueEle (ele) Priority) / / empty direct queue if (this.isEmpty ()) {items.push (element)} else {var qeueued = false / / whether the priority requirements are met and have joined the for (let I = 0; I < this.size (); items +) {if (element.priority < items [I] .priority) {items.splice (I, 0, element) qeueued = true break } / / if you don't meet the requirements, you don't join the team as required. Then enter the queue directly from the tail if (! qeueued) items.push (element)}} / / dequeue this.dequeue = function () {return items.shift ()} / / return the first element this.front = function () {return items [0]} / / whether the queue is empty this.isEmpty = function () { Return items.length = = 0} / / clear queue this.clear = function () {items = []} / / return queue length this.size = function () {return items.length} / / View queue this.show = function () {return items}} var priorityQueue = new PriorityQueue () PriorityQueue.enqueue ('priority 2-1', 2); priorityQueue.enqueue ('priority 1-1', 1); priorityQueue.enqueue ('priority 1-2', 1); priorityQueue.enqueue ('priority 3-1', 3); priorityQueue.enqueue ('priority 2-2, 2); priorityQueue.enqueue (' priority 1-3', 1); priorityQueue.show () / / output in priority order [0:queueEle {ele: "priority 1-1", priority: 1}, 1:queueEle {ele: "priority 1-2", priority: 1}, 2:queueEle {ele: "priority 1-3", priority: 1}, 3:queueEle {ele: "priority 2-1", priority: 2}, 4:queueEle {ele: "priority 2-2", priority: 2} 5:queueEle {ele: "priority 3-1", priority: 3}] Thank you for reading this article carefully. I hope the article "how to realize the js priority queue" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report