JS algorithm and data structure - queue

tags: JavaScript data structure and algorithm  data structure  algorithm  javascript  JS queue

queue

Queue structure

Creation of Queues - Array Method

Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Package queue</title>
</head>
<body>
  <script>
    function Queue(){
      // Attributes
      this.items = [];
      // method
      / / 1. Add the elements to the queue
      Queue.prototype.enqueue = function(element){
        this.items.push(element);
      };
      // 2. Remove the front end element from the queue
      Queue.prototype.delqueue = function(element){
        / / Remember that there is a return value here, it is undefined because the function default return value is undefined.
        return this.items.shift(element);
      };
      // 3. View front end elements
      Queue.prototype.front = function(){
        return this.items[0];
      }

      // 4. Check if the queue is empty
      Queue.prototype.isEmpty = function(){
        return this.items.length == 0;
      }

      // 5. View the number of elements in the queue
      Queue.prototype.size = function(){
        return this.items.length;
      }

      // 6.toString method
      Queue.prototype.toString = function(){
        return this.items.join("");
      }
    }

    let queue = new Queue();
    console.log(queue.isEmpty());
    console.log(queue.size());
    queue.enqueue(1);
    queue.enqueue(10);
    queue.enqueue(8);
    queue.enqueue(7);
    console.log(queue);
    console.log(queue.front());
    console.log(queue.isEmpty());
    console.log(queue.size());
    console.log(queue.toString());
    queue.delqueue();
    console.log(queue);
    console.log(queue.front());
    console.log(queue.isEmpty());
    console.log(queue.size());
    console.log(queue.toString());
  </script>
</body>
</html>

The actual use of the queue - drumming flowers

topic:

Here is actually not thinking about it and the queue is on, it is recommended to see the code, readers will think about it, what will they do when I do it!

If you want to take the queue, you have to think about it, then remove it, then remove each time, then you will follow!

Code

// Top Test: Drumming
function passGame(nameList,num){
  // 1 Create a queue
  let queue1 = new Queue();
  // 2 pass the value to the queue
  for(i of nameList){
    console.log(i);
    queue1.enqueue(i);
  }
  // I have been looping to only one person.
  while(queue1.size() > 1){
    // People before the number re-join the queue
    // The number of readers in the array may not be like me. In fact, this is not used to access the subscription, but it is always looped, and the operation is given Queue.
    for(let i = 0;i<num-1;i++){
      queue1.enqueue(queue1.delqueue())
    }
    // NUM corresponding to people, delete directly
    queue1.delqueue();
  }
  console.log(queue1.front());
  return nameList.indexOf(queue1.front());
}
let arr = ["1","2","3","4","5"];
console.log(passGame(arr,3));

Intelligent Recommendation

JS [JavaScript] data structure and algorithm of the queue

Many front-end engineers areWithout solid foundation, Little knowledge of data structures and algorithms, or worse still have "I write a page, what algorithm would use" similar mentality. Th...

Data structure and algorithm queue (JS description)

What is a queue Queue is a first-in-first-out orderly structure. The most common thing in our lives is to queue up, and whoever comes first will serve the first. You can often see it in the program, s...

js data structure and algorithm (2) stack and queue

1. Stack 1. Definition The stack is an important linear structure. Stack is aLast in first out(Last in first out, LIFO) linear table, it requires only delete and insert operations at the end of the ta...

More Recommendation

3. Queue of js data structure and algorithm

1 Introduction Queue is a first-in-first-out (First-In-First-Out, FIFO) data structure. Queues are used in many places, such as submitting a series of processes executed by the operating system, print...

Data structure and algorithm JS description stack and queue

Data structure and algorithm JS description stack and queue Reference Data structure and algorithm JS description, [America] Michael McMillan, translated by Wang Qunfeng and Du Huan Write in front The...

Data structure and algorithm (js description) --- queue

Definition of queue Like the stack, the queue is a linear list with limited operations. But unlike the stack, the queue is only allowed to insert at one end of the table and delete at the other end of...

Data structure and algorithm - JS implementation (stack and queue)

JavaScript implementation stacks and queues This implementation is relatively simple, there is no function. Stack queue Stack queue...

[JS data structure and algorithm] Queue, encapsulation and application of queue

1. What is a queue? Queue is a special linear table, First In First Out (FIFO, First In First Out) The special feature is that it only allows delete operations at the front of the table. While inserti...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top