


Detailed explanation of JavaScript priority queue and circular queue examples
This article mainly introduces the priority queue and circular queue of the JavaScript data structure. It analyzes the principles, definitions and usage of the priority queue and circular queue in the javascript data structure in detail in the form of examples. Friends in need can refer to it. I hope Can help everyone.
Priority Queue
Implement a priority queue: set the priority, then add elements at the correct position.
What we implement here is a minimum priority queue, and elements with small priority values (high priority) are placed in front of the queue.
//创建一个类来表示优先队列 function Priorityqueue(){ var items=[];//保存队列里的元素 function QueueEle(e,p){//元素节点,有两个属性 this.element=e;//值 this.priority=p;//优先级 } this.enqueue=function(e,p){//添加一个元素到队列尾部 var queueEle=new QueueEle(e,p); var added=false; //priority小的优先级高,优先级高的在队头 if(this.isEmpty()){ items.push(queueEle); }else{ for(var i=0;i<items.length>queueEle.priority){ items.splice(i,0,queueEle); added=true; break; } } if(!added){ items.push(queueEle); } } } this.isEmpty=function(){ return items.length==0; } this.dequeue=function(){ return items.shift(); } this.clear=function(){ items=[]; } this.print=function(){ console.log(items); } this.mylength=function(){ return items.length; } } var pqueue=new Priorityqueue(); pqueue.enqueue('a',2); pqueue.enqueue('b',1); pqueue.enqueue('c',2); pqueue.enqueue('d',2); pqueue.enqueue('e',1); pqueue.print(); //[ QueueEle { element: 'b', priority: 1 }, // QueueEle { element: 'e', priority: 1 }, // QueueEle { element: 'a', priority: 2 }, // QueueEle { element: 'c', priority: 2 }, // QueueEle { element: 'd', priority: 2 } ]</items.length>
Running results:
Add elements at the correct position: If the queue is empty, you can directly enqueue the elements. Otherwise, the priority of this element needs to be compared with other elements. When an item with a lower priority than the element to be added is found, the new element is inserted before it. In this way, for other elements with the same priority but added to the queue first, we also follow the first-in-first-out principle.
Maximum priority queue: Elements with larger priority values are placed at the front of the queue.
Circular Queue
Implement the drumming and flower passing game.
//创建一个类来表示队列 function Queue(){ var items=[];//保存队列里的元素 this.enqueue=function(e){//添加一个元素到队列尾部 items.push(e); } this.dequeue=function(){//移除队列的第一项,并返回 return items.shift(); } this.front=function(){//返回队列的第一项 return items[0]; } this.isEmpty=function(){//如果队列中部包含任何元素,返回true,否则返回false return items.length==0; } this.mylength=function(){//返回队列包含的元素个数 return items.length; } this.clear=function(){//清除队列中的元素 items=[]; } this.print=function(){//打印队列中的元素 console.log(items); } } //击鼓传花 function hotPotato(namelist,num){ var queue=new Queue(); for(var i=0;i<namelist.length>1){ for(i=0;i<num var console.log><p>Run result: </p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/054/025/35fe6991be3b65a9e5776299bc6a0f97-1.jpg?x-oss-process=image/resize,p_40" class="lazy" alt=""></p> <p>Get a list and add all the names in it to the queue. Given a number, the queue is iterated over. Remove an item from the head of the queue and add it to the tail of the queue to simulate a circular queue. Once the number of passes reaches a given number, the person who got the flower is eliminated. When there is only one person left in the end, he is the winner. </p> <p>Related recommendations: </p> <p><a href="http://www.php.cn/java-article-361900.html" target="_self">Detailed explanation of how priority queue is implemented in JDK</a></p> <p><a href="http://www.php.cn/php-weizijiaocheng-290402.html" target="_self">PHP data structure queue (SplQueue) and priority queue ( SplPriorityQueue) simple usage example_PHP tutorial</a></p> <p><a href="http://www.php.cn/js-tutorial-22701.html" target="_self">Circular queue code implemented using arrays in javascript_javascript skills</a></p></num></namelist.length>
The above is the detailed content of Detailed explanation of JavaScript priority queue and circular queue examples. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
