Implementing queues in js

Queue is a first-in-first-out data structure. The main operations of queue are inserting new elements into the queue and deleting elements in the queue, inserting new elements at the end of the queue and deleting elements at the head of the queue. Another important operation is to read the element peek of the queue head, the underlying data structure and Using stack in js As with arrays, you can simplify the code here by using the powerful shift and push methods in js array objects. The constructor of the queue is as follows:

   function Queue(){
       this.dataStore = [];
       this.enqueue = enqueue;
       this.dequeue = dequeue;
       this.front = front;
       this.back = back;
       this.toString = toString;
       this.empty = empty;
   }

Method realization

The enqueue method adds an element to the end of the queue

   function enqueue(element){
       this.dataStore.push(element);
   }

The dequeue method deletes the elements of the team head

   function dequeue(){
       this.dataStore.shift();
   }

Read the elements of the head and tail of the team in the following way

   function front(){
       return this.dataStore[0];
   }
   function back(){
       return this.dataStore[this.dataStore.length - 1];
   }

The toString method displays elements in the queue

   function toString(){
       var str = '';
       for(var i = 0, i = this.dataStore.length; i < len; i++){
           str += this.dataStore[i] + '\n';
       }
       return str;
   }

Empty determines whether the queue is empty

   function empty(){
       return this.dataStore.length == 0;
   }

Method use

Queue sorting is called cardinal sorting. For 0-99 digits, the data set is scanned twice, sorted by digits in single digits for the first time and sorted by digits in ten digits for the second time.

   function distribute(nums, queues, n, digit){
        //The digit parameter represents values on ten or more bits
        //Use ten queues to temporarily save the sorting results, n being the length of the array to be sorted
        //When digit=1, after the first sorting, the array is ordered in bits
        //Because of the first-in, first-out principle, the smaller number in the second ranking rank is ahead of the ten ranking ranks.
    for(var i = 0; i < n; i++){
        digit == 1 ?
        queues[nums[i] % 10].enqueue(nums) :
        digit == 10 ? 
        queues[Math.floor(nums[i] / 10)].equeue(nums)
    }
   }
   function collect(queues, nums, n){
       //Change nums arrays because nums saves references to arrays
       var i = 0;
       for(var j = 0; j < n; j++){
          while(!queues[j].empty()){
             num[i++] = queues[j].dequeue();
          }
       }
   }

In the call, create 10 queues first

   var queues = [];
   //Array queues hold ten queues for sorting
   for(var i = 0; i < 10; i++){
       queues[i] = new Queue();
   }
   //nums is the array to be sorted
   var nums = [9146851592353122];
   distribute(nums, queues, nums.length, 1);
   collect(queues, nums, nums.length);
   // nums = [91, 31, 92, 22, 85, 15, 35, 46];
   distribute(nums, queues, nums.length, 10);
   collect(queues, nums, nums.length);
   //nums = [15, 22, 31, 35, 46, 85, 91, 92];

Priority queue

In general, the elements deleted in the queue must be the first elements to join the queue. But some applications that use queues need to use a data structure called priority queues to simulate deleting elements that do not want to comply with this convention.
When deleting elements from priority queues, priority restrictions, such as VIP users queued in banks, need to be considered.

   function Rank(){
      this.name = name;
      //Significance of variable code identification
      this.vipCode = vipCode;
   }

Now redefining the dequeue method, we agree that the element with the smallest code value has the highest priority.

   function dequeue(){
       var entry = 0;
       for(var i = 1; i < this.dataStore.length; i++){
          entry = this.dataStore[i].vipCode < this.dataStore[entry].vipCode ? i : entry;
       }
       //splice method changes array directly
       return this.dataStore.splice(entry, 1);
   }

In use, the basic elements of the stack are constructed by Rank

    var customer1 = new Rank('John', 5);
    var customer2 = new Rank('Merry', 1);
    var queue = new Queue();
    queue.enqueue(customer1);
    ...

Posted by woozy on Wed, 27 Mar 2019 06:51:30 -0700