In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use js circular queue", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use js circular queue" this article.
1. You can use a circular queue to simulate the game of beating drums and passing flowers (Joseph's ring problem).
2. A group of children form a circle, passing n at a time, and the children with flowers in their hands are eliminated when they stop, until there is only one child left in the team, that is, the winner.
In a circular queue, a child pops up each time (from the head of the queue), and then the child is added to the end of the queue. The child who pops up at the head of the queue (eliminated) when the cycle stops until there is only one child left in the queue.
Example
Function Queue () {/ / initialize the queue (implemented using an array) var items = []; / / join the queue this.enqueue = function (ele) {items.push (ele);}; / / dequeue this.dequeue = function () {return items.shift ();}; / / return the first element this.front = function () {return items [0];} / / whether the queue is empty this.isEmpty = function () {return items.length = = 0;}; / / clear queue this.clear = function () {items = [];}; / / return queue length this.size = function () {return items.length;}; / / View queue this.show = queue () {return items;} } / * @ param {list} names * @ param {specify the number of passes} num * / function onlyOne (names, num) {var queue = new Queue (); / / all lists join the names.forEach ((name) = > {queue.enqueue (name);}); / / the name of the eliminated person var loser = "" / / as long as there is more than one person around, it will continue to while (queue.size () > 1) {for (let I = 0; I < num; iTunes +) {/ / rejoin the team for a total of num times (num times in total) queue.enqueue (queue.dequeue ()) Loser = queue.dequeue (); console.log (loser + "eliminated");} / there is only one person left return queue.dequeue ();} var names = ["Liberal Arts", "Zhang Fan", "Qin Jun", "Qiu Qiu", "Huang Jing"]; var winner = onlyOne (names, 99) Console.log ("Golden Horse Award winner:" + winner); above is all the content of this article "how to use the js Loop queue". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 203
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.