My uexi for JS queues

Keywords: Javascript less

My study of JS queues

What is queue

Queues are a set of ordered items that follow the FIFO principle. Queues add new elements at the tail and remove elements from the top. The newly added elements must be at the end of the queue.

In practical applications, it is usually implemented by linked lists or arrays.

Queue learning

The operation of the queue is actually similar to that of the stack, but the queue allows only new data to be added at the back end.

1 Create queues

Declare a class

function Queue() {
    //Here are attributes and methods
}

You need a data structure for storing elements in the queue, where you select an array.

var items = [];

Basic operation of queue 2

  • Add elements to the queue (only to the end of the queue)

this.enqueue = function (element) {
    items.push(element);
}
  • Remove the first item of the queue and return the removed element

this.dequeue = function() {
    return items.shift();
}
  • Get the front element of the queue

this.front = function() {
    return items[0];
}
  • Determine whether the queue is empty

this.isEmpty = function() {
    return items.length == 0;
}
  • Queue length

this.size = function() {
    return items.length;
}

The difference between queues and stacks is the dequeue() method and the front() method, because the first-in, first-out and last-in, first-out principles are different.

Use Queue classes

1. Initialize the class to verify that it is empty

var queue = new Queue();
console.log(queue.isEmpty()); //true;

2. Add to the queue to determine the length

queue.enqueue("John");
queue.enqueue("Jack");
queeu.enqueue("Camila");

console.log(queue.size()); //3

3. Remove two elements and determine length

queue.dequeue(); 
queue.dequeeu(); 

console.log(queue.size()); //1; At this point, only Camila is left in the queue.

Priority queue

Yes, that's the privilege! For example, boarding, first-class business class and economy class boarding order? Surely they have a high priority.

To achieve a priority queue, there are two options: (1) Set priority and add elements in the right place. (2) Add elements with column entry operations, and then remove them according to priority.

Boarding is boarding. You can let the first priority go to the waiting room, and then boarding in the first class, which is the order of course. The other is that first-class economy class or something does not enter the waiting room according to priority, but when boarding, people are called in according to priority.

function PriorityQueue() {
    var items = [];

//Create an element that contains elements and priorities
    function QueueElement (element, priority) {
        this.element = element;
        thie.priority = priority;
    };

    this.queue = function(element, priority) {
        var queueElement = new QueueElement(element, priority);

//The queue is empty and added directly to the queue (because there is no need to compare priorities at this time).
        if(this.isEmpty()) {
            items.push(queueElement);
        } else {
            var added = false;
            //If the priority of the element to be added is less than that of a location element, place the element to be added in front of the location element (splice method is used here).
            //The smaller the priority number, the higher the priority, and the earlier it should be processed, so it should be closer to the front of the queue.
            for(var i = 0; i < items.length; i++) {
                if(queueElement.priority < items[i].priority) {
                    items.splice(i, 0, queueElement);
                    added = true;
                    //After inserting a new element, terminate the queue loop
                    break;
                }
            }
            //This means that all elements have a lower priority than the elements to be added, so you can just put them at the end.
            if(!added) {
                items.push(queueElement);
            }
        }
    };
}
var priorityQueue = new PriorityQueue();

priorityQueue.enqueue("John", 2);
priorityQueue.enqueue("Jack", 1);
priorityQueue.enqueue("Camila", 1);

The resulting queue is | Jack(1)|Camila(1)|John(2) | with the same priority, just after the same priority.

Here is the minimum priority queue, and elements with smaller priority values are placed at the front of the queue.

Circular queue

Drum-beating and flower-passing, children form a circle, the flowers as soon as possible to the people next to, at a certain moment, flower-passing stopped, at this time, who spent in the hands of the elimination. Repeat this process until only one child is left.

function hotPotato(nameList, num) {
    var queue = new Queue();

//Put all the names in the queue
    for(var i = 0; i < nameList.length; i++) {
        queue.enqueue(nameList[i]);
    }

    var eliminated = '';
    //One round of games eliminates one person
    while(queue.size() > 1) {
        //7 drums in a round of games
        for(var i = 0; i < num; i++) {
            //Once you pass the drum, you pass the flowers to others, and you are safe.
            //Remove an item from the beginning of the queue and put it at the end of the queue
            queue.enqueue(queue.dequeue());
        }
        //At the end of a game, eliminate the person who holds the flower in his hand, that is, the leader of the team.
        eliminated = queue.dequeue();
        console.log(eliminated + "Be eliminated");
    }

    //Get the last team leader
    return queue.dequeue();
}

var names = ["John", "Jack", "Camila", "Ingrid", 'Carl'];
var winner = hotPotato(names, 7);
console.log('Winner' + winner);

Result:
Camila Jack Carl Ingrid was eliminated in turn
Winner: John

Next list...

Posted by ucffool on Wed, 17 Apr 2019 17:39:34 -0700