1. Understanding Queues The previous blog has talked about the restricted data structure - the stack. Now, let’s take a look at the
Its structure diagram can be expressed as:
2. Encapsulation QueueThe queue structure is also implemented in an array format. First, create a class. function Queue(){ } Add properties and methods inside it, and add the array to the class through the property methods. Then use the prototype method to add commonly used operations. Common operations on queues are:
Now let’s implement it: function Queue(){ this.items = []; //Add one (or more) new items to the end of the queue enqueue() Queue.prototype.enqueue = function(element){ this.items.push(element); } //Remove the first item in the queue (i.e. the front item in the queue) dequeue() Queue.prototype.dequeue = function(){ return this.items.shift(); } //Return the first element in the queue front() Queue.prototype.front = function() { return this.items[0]; } // Check if the stack is empty isEmpty() Queue.prototype.isEmpty = function(){ return this.items.length == 0; } //Return the number of elements contained in the queue size() Queue.prototype.size = function(){ return this.items.length; } //Convert the contents of the queue into a string form toString() Queue.prototype.toString = function(){ var str = ''; for(var i =0;i<this.items.length;i++){ str += this.items[i] + ' '; } return str; } } The above is the encapsulation of the queue, now verify it: var queue = new Queue(); queue.enqueue(10); queue.enqueue(20); queue.enqueue(30); queue.enqueue(40); queue.enqueue(50); console.log(queue); console.log('The first item removed is: ' + queue.dequeue()); console.log('The first element in the queue is: ' + queue.front()); console.log('Is the stack empty: '+queue.isEmpty()); console.log('The contents of the stack structure are:'); console.log(queue.toString()); The output is: Build successful. Let’s take a look at a case of passing the parcel while beating the drum! 3. The Case of Passing the Flower by Beating the DrumOriginal game rules:
Modify the game rules:
Encapsulate a queue-based function:
The code is as follows: // Encapsulate queue function Queue(){ this.items = []; //Add elements at the end Queue.prototype.enqueue = function(element){ this.items.push(element); } //Remove the first element Queue.prototype.dequeue = function(){ return this.items.shift(); } //Return the first element Queue.prototype.front = function(){ return this.items[0]; } //Return the number of elements contained in the queue Queue.prototype.size = function(){ return this.items.length; } } function passGame(nameList,num){ // Create a queue var queue = new Queue(); //Add all people to the queue for(var i = 0;i<nameList.length;i++){ queue.enqueue(nameList[i]); } //Play the game while (queue.size() > 1) { //The people before num are added to the end of the queue for(var i =1;i<num;i++){ queue.enqueue(queue.dequeue()); } //People with num numbers are directly removed from queue.dequeue(); } //Get the winner information var endName = queue.front(); console.log('The remaining people are:' + endName); return nameList.indexOf(endName); } //Test var nameList = ['a','b','c','d','e']; var g = passGame(nameList,5); console.log('This person's location is: '+g); The output is: This is the end of this article about the process of implementing queue structure in JavaScript. For more relevant content about implementing queue structure in JavaScript, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to do a good refactoring is not only refactoring the code but also refactoring life
>>: Introduction to basic concepts and technologies used in Web development
1. Error details Once when manually performing a ...
Table of contents Single content projection Multi...
Table of contents 1 View the current database con...
Absolute positioning method: (1) Set the parent e...
Table of contents Basic application of javascript...
Prepare the bags Install Check if Apache is alrea...
1. Common usage: (1) Use with % % represents a wi...
Operating system: windowns10_x64 Python version: ...
html <div class="totop" v-show="...
[ Linux installation of Tomcat8 ] Uninstall Tomca...
Problem Peeping In the server, assuming that the ...
1. Service method Check the firewall status: [roo...
Table of contents 1. Function Introduction 2. Key...
How to install MySQL 5.7 in Ubuntu 16.04? Install...
In daily work, we sometimes run slow queries to r...