Priority queue and loop queue of JavaScript data structure

Keywords: Javascript

Priority queue and loop queue of JavaScript data structure

Priority queue

Priority queue is to add and delete queues based on priority

The minimum priority queue (priority addition, normal dequeue) code is as follows:

 function Queue() {
        //Initialize queue (using array implementation)
        var items = [];
        //Insert element into queue (tail)
        this.enqueue = function(element) {
            items.push(element);
        }
        //Pops an element from the queue (header) and returns it
        this.dequeue = function() {
            return items.shift();
        }
        //View the first element of the queue (element with index 0 in the array)
        this.front = function() {
            return items[0];
        }
        //Check whether the queue is empty. If it is empty, return true; otherwise, return false
        this.isEmpty = function() {
            return items.length == 0;
        }
        //View the length of the queue
        this.size = function() {
            return items.length;
        }
        //View queue
        this.print = function() {
            //Return as string situation
            return items.toString();
        }
    }
	 function PriorityQueue() {
	        var items = [];
	        //The element that needs to be inserted into the queue (the element is an object, including values and priorities)
	        function QueueElement(element, priority) {
	            this.element = element;
	            this.priority = priority;
	        }
	
	        //How to insert an element into a queue
	        this.enqueue = function (element, priority) {
	            //Elements that need to be inserted into the queue
	            var queueElement = new QueueElement(element, priority);
	
	            if(this.isEmpty()) {
	                //When the queue is empty, add elements directly to the queue
	                items.push(queueElement);
	            }else{
	                //When the queue is not empty, traverse the elements in the queue. When the priority of the element to be added is lower than that of the current element (in the queue), insert the element before the current element
	                var added = false;
	                for(var i = 0; i < items.length; i++){
	                    if(queueElement.priority < items[i].priority) {
	                        items.splice(i, 0, queueElement);
	                        added = true;
	                        break;//End queue loop
	                    }
	                }
	                //When the priority of the element to be added is higher than that of any element in the queue, insert the element at the end of the queue
	                if(!added){
	                    items.push(queueElement);
	                }
	            }
	        }
	
	        //Check whether the queue is empty. If it is empty, return true; otherwise, return false
	        this.isEmpty = function() {
	            return items.length == 0;
	        }    
	
	        //View queue
	        this.print = function() {
	            return items;
	        }        
	    }
	    var priorityQueue = new PriorityQueue();
	    priorityQueue.enqueue('a', 10);
	    priorityQueue.enqueue('b', 3);
	    priorityQueue.enqueue('c', 8);
	    priorityQueue.enqueue('d', 8);
	    priorityQueue.enqueue('e', 20);
	    console.log(priorityQueue.print());

Circular queue

The so-called cyclic queue is to pop up a value at each cycle (from the queue head), and then add this value to the end of the queue, cycle n times (custom n value), pop up the value at the queue head when the cycle stops, until there is only one value left in the queue.
The code is as follows:

function Queue() {
        
        //Initialize queue (using array implementation)
        var items = [];

        //Insert element into queue (tail)
        this.enqueue = function(element) {
            items.push(element);
        }

        //Pops an element from the queue (header) and returns it
        this.dequeue = function() {
            return items.shift();
        }

        //View the first element of the queue (element with index 0 in the array)
        this.front = function() {
            return items[0];
        }

        //Check whether the queue is empty. If it is empty, return true; otherwise, return false
        this.isEmpty = function() {
            return items.length == 0;
        }

        //View the length of the queue
        this.size = function() {
            return items.length;
        }

        //View queue
        this.print = function() {
            //Return as string situation
            return items.toString();
        }
    }
    //nameList array
    //num specified number of passes
    function hotPotato(nameList, num) {

        var queue = new Queue();

        //Insert list into queue
        for(var i = 0; i < nameList.length; i++) {
            queue.enqueue(nameList[i]);
        }

        //Initial value
        var eliminated = '';

        //Continue delivery when the number of queues is greater than 1
        while(queue.size() > 1) {
            for(var i = 0; i < num; i++) {
                //Insert the pop-up value of the queue head into the end of the queue again each time, and travel a circular queue
                queue.enqueue(queue.dequeue());
            }
            //When the loop stops, that is, the specified number of times, the value of the pop-up queue header
            eliminated = queue.dequeue();
            console.log(eliminated + 'Eject');
        }

        //End with last value left
        return queue.dequeue();
    }

    var names = ['a', 'b', 'c', 'd', 'e'];
    var last = hotPotato(names, 5);
    console.log("Last value left:"+last);

Published 91 original articles, won praise 198, visited 10000+
Private letter follow

Posted by upnxwood16 on Tue, 17 Mar 2020 09:24:20 -0700