JavaScript queue, priority queue and cyclic queue, and javascript queue

Source: Internet
Author: User

JavaScript queue, priority queue and cyclic queue, and javascript queue

A queue is an ordered set that complies with the FIFO principle.
Add new elements to the end of the queue and remove elements from the top.

Queue understanding

Queuing is the most common scenario in our life.
The queue name is easy to understand.

This is similar to the stack, but the queue is the first-in-first-out data structure.

The front of the queue is the head of the queue.
The queue is followed by the team end
Out of the queue
Enter from the end of the team

Create a queue

Similar to the stack, I won't be so embarrassed here.
You also need to implement some functions
Here I like queuing up for the toilet in my life.

  • Add elements to the queue (into the queue)
  • Remove the team Header element (the front of the team goes out to the toilet)
  • View the team Header element (view the person at the top of the Team)
  • Determine whether the queue is empty (check whether there are any Members in the Team)
  • Remove all elements from the Team (when the toilet is blown up, all are scattered)
  • View the number of elements in the stack (view the number of queues)

So we can create a complete queue implementation, also using our Array Implementation
The array header is the queue header.

Function Queue () {var items = []; this. enqueue = function (ele) {items. push (ele) ;}; // join this. dequeue = function () {return items. shift () ;}; // this. front = function () {return items [0] ;}; // view the team Header element this. isEmpty = function () {return items. length = 0;}; // determines whether the queue is empty. this. size = function () {return items. length ;}; // queue size this. clear = function () {items = [] ;}; // clear the queue this. print = function () {console. log (items. toString () ;}; // print queue} var Queue = new queue (); // declare the Queue instance

Queue usage

Next, we will use this queue to simulate queuing.

Var queue = new Queue (); console. log ("Whether the 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 ("outsourcers:" + queue. dequeue (); console. log ("current queue:"); queue. print ();

Console printing:


Priority queue

When we waited for the bathroom, a friend with a VIP membership card came to the front of the team.
After a while, another friend with an SVIP membership card was inserted in front of the VIP.

Although this metaphor may be inappropriate, there may be queues with priorities in life.
The person with a higher priority can find the person with a lower priority.

This isCyclic queue

If the element with a smaller priority value is placed before the queue, this is calledMinimum priority queue
The element with a higher priority value is placed before the queue, which is calledMaximum priority queue
But in fact, the two of them are only a change in judgment, and the implementation method is the same.
The difference between a priority queue and a normal queue is that the priority should be determined for the queue, and our elements must be processed. Other methods remain unchanged.
This process encapsulates an element as an object with a priority.
Since all objects share the same attributes, we should no doubt use the factory to build them.
We can slightly modify our queue class.
To implement a minimum priority queue

Function PriorityQueue () {var items = []; function QueEle (ele, priority) {// encapsulate our elements as an object this. ele = ele; // element this. priority = priority; // priority} this. enqueue = function (ele, priority) {var queObj = new QueEle (ele, priority); // create a queue element object if (this. isEmpty () {// If the queue is empty, insert this directly. 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); // cyclic queue. if the priority is lower than the priority of the element in this position, insert bAdded = true; break ;}} if (! BAdded) {items. push (queObj); // if no position can be inserted in a circle, insert it directly to the end of the queue}; 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 () {// modify var temp = []; for (var I = 0, len = items. length; I <len; I ++) {temp. push (items [I]. ele);} console. log (temp. toString ());};}

I have already explained it in the code.
Next we will use this priority queue to simulate the WC queue.

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:


Cyclic queue

Typical Example of cyclic queue
I still remember when I was in high school, we went to school late and had a power outage.
Take a things as a "Flower", and turn around for a pass. When the "drum" stops, the students who get the flowers will stand up and sing.
The cyclic queue can be considered as a queue application.
Next we will simulate the implementation of cyclic queue drumming

Function hotPotato (pepoleList, frequency) {// parameter: represents the array of people, the frequency of flower transmission var queue = new Queue (); for (var I = 0, len = pepoleList. length; I <len; I ++) {queue. enqueue (pepoleList [I]); // initialize and enter the queue} var eliminated; // when (queue. size ()> 1) {// as long as there are at least two other people in the queue, it will keep repeating for (var I = 0; I <frequency; I ++) {// enter the queue, simulate the loop effect queue. enqueue (queue. dequeue ();} eliminated = queue. dequeue (); // liquidation console. log (eliminated + 'obsolete ');} return queue. dequeue (); // return the last person in the queue} var pepole = ['Mr. a', 'Mr. B ', 'Mr. c', 'Mr. d', 'Mr. E ', 'Mr. f']; var gameWinner = hotPotato (pepole, 12); console. log ('best audience: '+ gameWinner );

Console output:

 

The above is the queue implementation under JavaScript.
We also briefly understand two special queues: priority queue and cyclic queue.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.