In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
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)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.