This time I will bring you the priority of parsing JS Queue and Circular queue, what are the precautions for parsing JS priority queue and circular queue, as follows This is a practical case, let’s take a look at it.
Priority Queue
Implement a priority queue: set the priority, then add the element at the correct position.
What we implement here is a minimum priority queue, and elements with small priority values (high priority) are placed at the front of the queue.
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 | function Priorityqueue(){
var items=[];
function QueueEle(e,p){
this.element=e;
this.priority=p;
}
this.enqueue= function (e,p){
var queueEle= new QueueEle(e,p);
var added=false;
if (this.isEmpty()){
items.push(queueEle);
} else {
for ( var i=0;i<items.length>queueEle.priority){
items.splice(i,0,queueEle);
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( 'a' ,2);
pqueue.enqueue( 'b' ,1);
pqueue.enqueue( 'c' ,2);
pqueue.enqueue( 'd' ,2);
pqueue.enqueue( 'e' ,1);
pqueue. print ();
|
Copy after login
operation result:

Add elements at the right position: If the queue is empty, you can directly enqueue the element. Otherwise, you need to compare the priority of this element with other elements. When an item with a lower priority than the element to be added is found, the new element is inserted before it. In this way, for other elements with the same priority but added to the queue first, we also follow the first-in-first-out principle.
Maximum priority queue: Elements with larger priority values are placed at the front of the queue.
Circular Queue
Realize the drumming and passing flowers game.
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 | function Queue(){
var items=[];
this.enqueue= function (e){
items.push(e);
}
this.dequeue= function (){
return items.shift();
}
this.front= function (){
return items[0];
}
this.isEmpty= function (){
return items.length==0;
}
this.mylength= function (){
return items.length;
}
this.clear= function (){
items=[];
}
this. print = function (){
console.log(items);
}
}
function hotPotato(namelist,num){
var queue= new Queue();
for ( var i=0;i<namelist.length>1){
for (i=0;i<num var console.log><p style= "text-align: left;" >
Get a list and add all the names to the queue. Given a number, the queue is iterated over. Remove an item from the head of the queue and add it to the tail of the queue to simulate a circular queue. Once the number of passes reaches a given number, the person who got the flower is eliminated. When there is only one person left in the end , he is the winner. </p>
<p> I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! </p>
<p>Recommended reading: </p>
<p><a href= "http://www.php.cn/js-tutorial-392833.html" target= "_blank" >Detailed explanation of the use of BootStrap's text editor component Summernote</a><br></p>
<p>##Operation search component is displayed on the keyboard<a href= "http://www.php.cn/js-tutorial-392831.html" target= "_blank" ></a><br></p></num></namelist.length>
|
Copy after login
The above is the detailed content of Parsing JS's priority queue and circular queue. For more information, please follow other related articles on the PHP Chinese website!