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 priority queue in JavaScript

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

Share

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

This article mainly explains "how to achieve priority queue in JavaScript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve priority queue in JavaScript".

I. introduction of priority queues

We know that a normal queue inserts an element, the data is placed on the back end, and all the previous elements need to be processed before the previous data is processed. However, the priority queue, when inserting an element, takes into account the priority of the data and compares it with the priority of other data. After the comparison is complete, you can find out the correct position of this element in the queue, and other processing methods are basically the same as the basic queue.

The main issues to be considered in priority queues:

Each element is no longer just a data, but also contains the priority of the data

In the add mode, put it in the correct position according to the priority.

There are also examples of priority queues used on a daily basis, such as the waiting room of a hospital. Doctors will give priority to patients who are seriously ill. In the computer, we can also rearrange the order of tasks in the queue through the priority queue. For example, the importance of the task handled by each thread is different, and we can determine the order in which the thread is processed in the queue by the size of the priority.

Priority queue encapsulation

The operation of the priority queue is basically the same as the operation of the queue, but the insertion operation is different, so we are mainly here to implement the priority queue insertion operation.

For example, we are now going to insert elements according to the priority of some data. here we first create a class to encapsulate the priority queue and create a constructor inside it to hold the priority and data of the element. then add an attribute to store the element.

The code is as follows:

Function PtiorityQueue () {var items = []; / / encapsulates a new constructor to hold the element and its priority function queueElement (element,priority) {this.element = element; this.priority = priority;}}

After the creation is complete, the insert operation is implemented:

If there are no elements inside the queue, insert directly

If the priority of the element to be inserted is less than that of the elements within the queue, it is sorted and inserted.

The specific implementation code is as follows:

Function PtiorityQueue () {this.items = []; / / encapsulates a new constructor that holds the element and its priority function QueueElement (element,priority) {this.element = element; this.priority = priority;} / / 1. Implement the insertion method PtiorityQueue.prototype.enqueue = function (element,priority) {/ / 1. Create a queueElement object var queueElement = new QueueElement (element,priority); / / 2. Determine whether the queue is empty if (this.items.length = = 0) {this.items.push (queueElement);} else {var flag = false; for (var I = 0polii)

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