Queues are an ordered set of advanced First Out (FIFO) principles
The queue adds a new element to the tail, removing the element from the top
The understanding of the queue
The most common scene in our lives is the queue.
The name of the queue is already very easy to understand.
It's like the stack, but the queue is a first-in-first-out data structure.
In front of the queue is the team head
Behind the queue is the end of the line
Out team head out of the team
The team from the tail into
Creation of queues
Like the stack, I'm not going to shut up here.
Also need to implement some features
Here I am analogy life in the line of the toilet
- Add an element to the queue (into the queued line)
- Remove team head elements (people in front of the team go into the toilet)
- View team head elements (see the person at the front of the queue)
- Determine if the queue is empty (see if there are any in the team)
- Remove all elements of the team (the toilet blew up, all scattered)
- View the number of elements in the stack (see how many people are queued)
So we can create a full queue implementation, and also use our array to implement
Array header is the queue header
function Queue () {
var items = [];
This.enqueue = function (ele) {
items.push (ele);
};/ /Brigade
This.dequeue = function () {return
items.shift ();
};/ /out team
This.front = function () {return
items[0];
};/ /view team head element
This.isempty = function () {return
items.length = = 0;
};/ /Determine if the queue is empty
this.size = function () {return
items.length;
};/ /Queue Size
this.clear = function () {
items = [];
};/ /empty queue
This.print = function () {
console.log (items.tostring ());
};/ /print queue
}
var queue = new Queue ();//Declare instance of queue
Use of queues
Here we'll use this queue to simulate the queues simply
var queue = new Queue ();
Console.log ("Queue is empty:" + queue.isempty ());
Queue.enqueue (' Mr.a ');
Queue.enqueue (' mr.b ');
Queue.enqueue (' mr.c ');
Console.log ("Current queue:");
Queue.print ();
Console.log ("Out Team Person:" + queue.dequeue ());
Console.log ("Current queue:");
Queue.print ();
Console printing:
Priority queues
While we were lining up for the bathroom, a friend with a VIP membership card was in the front of the team.
After a while again a friend with a SVIP membership card, inserted in front of the VIP
Although this analogy may be inappropriate, there may be a priority queue in life
High priority can be found in front of people with low priority.
This is the loop queue .
If an element with a small priority is placed in front of the queue, this is called the minimum precedence queue
Conversely, the elements with large priority values are placed in front of the queue, which is called the maximum precedence queue
But the fact that they're both just a change in judgment, the way it is implemented is the same
The difference between a priority queue and a normal queue is that the team has to prioritize, and we need to deal with our elements, and other methods will not change.
This process is to wrap the element as a priority object
Since all objects have the same attributes, we should definitely use the factory building
We can modify our queue Class A little bit
To implement a minimum priority queue
function Priorityqueue () {var items = []; function Queele (ele, priority) {//encapsulate our element as an object this.ele = ele;//element this.priority = priority;//Priority} This.enq Ueue = function (Ele, priority) {var queobj = new Queele (ele, priority);//Create Queue Element Object if (This.isempty ()) {//if queue is empty
, directly inserting This.push (queobj);
}else{var badded = false; for (var i = 0, len = items.length i < len; i++) {if (priority < items[i].priority) {Items.splice ( I, 0, queobj);
Loop queue, Insert badded = True if priority is less than the priority of this position element;
Break
} if (!badded) {Items.push (queobj);///If the loop does not find a place to jump in line, directly insert queue Tail}};
This.dequeue = function () {return items.shift ();
};
This.front = function () {return items[0];
};
This.isempty = function () {return items.length = = 0;
};
This.size = function () {return items.length;
};
This.clear = function () {items = [];
}; This.print = functIon () {//This place slightly modifies the next var temp = [];
for (var i = 0, len = items.length i < len; i++) {Temp.push (Items[i].ele);
} console.log (Temp.tostring ());
};
}
Explain what I've already said in the code.
Let's use this priority queue to simulate the queuing WC.
var pqueue = new Priorityqueue ();
Pqueue.enqueue (' Mr.a ', 3);
Pqueue.enqueue (' Mr.b ', 3);
Pqueue.enqueue (' Mr.C ', 3);
Console.log ("Original queue:");
Pqueue.print ();
Pqueue.enqueue (' VIP ', 2);
Pqueue.enqueue (' Svip ', 1);
Console.log ("New queue:");
Pqueue.print ();
Console printing:
Looping queues
loop queues Typical examples of drumming flowers
Remember when I was in high school, we had a blackout and we played this.
Take a thing when "flower", round biography, "Drum" a stop, get flowers students will stand up and sing
You can think of a loop queue as an application of a queue
Now let's simulate the loop queue, beat the drums, pass the flowers
function Hotpotato (pepolelist, frequency) {//parameter: Represents the human array, the frequency of the transmission
var queue = new Queue ();
for (var i = 0, len = pepolelist.length i < len i++) {
queue.enqueue (pepolelist[i]);//Initialize, enter queue
}
var Elimi Nated;//'s eliminated classmate while
(Queue.size () > 1) {//As long as there are at least two queues, loop for
(var i = 0; i < frequency; i++) {//out team, Simulate cyclic effect
queue.enqueue (Queue.dequeue ());
}
eliminated = Queue.dequeue ()//Liquidation
Console.log (eliminated + ' eliminated ');
}
return Queue.dequeue ()//returns the last person in the queue
.
var pepole = [' Mr.a ', ' mr.b ', ' mr.c ', ' mr.d ', ' mr.e ', ' mr.f '];
var gamewinner = Hotpotato (Pepole,);
Console.log (' The best: ' + Gamewinner ');
Console output:
The above is the queue implementation under JavaScript.
We also have a simple understanding of two special queues: Precedence queues and loop queues.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.