Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement priority queue and cyclic queue of JavaScript data structure

Shulou Source: shulou.com Published: 2022-05-31 12:40:58 09月30日 Update

This article focuses on "how to implement priority queue and circular queue of JavaScript data structure". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to implement priority queues and circular queues of JavaScript data structures.

Priority queue

Implement a priority queue: set the priority and then add the element in the correct location.

What we implement here is the minimum priority queue, and the elements with a low priority (high priority) are placed in front of the queue.

/ / create a class to represent the priority queue function Priorityqueue () {var items= []; / / save the element function QueueEle (eMaginp) {/ / element node in the queue, with two attributes this.element=e;// value this.priority=p;// priority} this.enqueue=function (eMagne p) {/ / add an element to the tail of the queue var queueEle=new QueueEle (eForce p); var added=false / / the small priority has a high priority, and the one with high priority is at the head of the line if (this.isEmpty ()) {items.push (queueEle);} else {for (var iQueueEle.priority) {items.splice (iMed 0QueueEle); added=true; break;}} if (! added) {items.push (queueEle) } this.isEmpty=function () {return items.length==0;} this.dequeue=function () {return items.shift ();} this.clear=function () {items= [];} this.print=function () {console.log (items);} this.mylength=function () {return items.length;}} var pqueue=new Priorityqueue (); pqueue.enqueue ('axiao Magazine 2); pqueue.enqueue (' baked Magazine 1); pqueue.enqueue ('Cruise Magne2) Pqueue.enqueue; pqueue.enqueue; pqueue.print (); / / [QueueEle {element: 'baked, priority: 1}, / / QueueEle {element:' estranged, priority: 1}, / / QueueEle {element: 'aforesaid, priority: 2}, / / QueueEle {element:' cased, priority: 2}, / QueueEle {element: 'dating, priority: 2}]

Running result:

Add the element in the right place: if the queue is empty, you can list the element directly. Otherwise, you need to compare the priority of this element with other elements. When you find an item that has a lower priority than the element to be added, insert the new element in front of it, so that we also follow the first-in, first-out principle for other elements with the same priority but added to the queue first.

Maximum priority queue: elements with high priority values are placed in front of the queue.

Circular queue

Realize the game of beating drums and spreading flowers.

/ / create a class to represent the queue function Queue () {var items= []; / save the element in the queue this.enqueue=function (e) {/ / add an element to the end of the queue items.push (e);} this.dequeue=function () {/ / remove the first item of the queue and return return items.shift ();} this.front=function () {/ / return the first item of the queue [0] } this.isEmpty=function () {/ / return true if there are any elements in the middle of the queue, otherwise return false return items.length==0;} this.mylength=function () {/ / return the number of elements contained in the queue return items.length;} this.clear=function () {/ / clear the element items= [] in the queue;} this.print=function () {/ / print the element in the queue console.log (items) }} / / beat the drum to spread the flower function hotPotato (namelist,num) {var queue=new Queue (); for (var item0 / 1) {for (iS0 / 10 / 1)

Tags: Queues elements priorities loops data data structures structures tails locations content numbers results floral learning running practical deeper smallest same two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno MariaDB NVidia Linux