当前位置:网站首页>JS queue
JS queue
2022-07-28 15:56:00 【PBitW】
List of articles
queue

Queue structure

Queue creation – Array methods

Code implementation
<!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> Encapsulation queue </title>
</head>
<body>
<script> function Queue(){
// attribute this.items = []; // Method // 1. Add 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 must be a return value here, otherwise the following drum beating and flower passing is undefined, Because the default return value of the function is undefined return this.items.shift(element); }; // 3. View front-end elements Queue.prototype.front = function(){
return this.items[0]; } // 4. See 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>
Practical application of queue – Beat the drum to spread the flowers
subject :
It's not easy to think about joining the queue here , It is recommended to look at the code , Readers think carefully first , What will you do when you do it yourself !
Want to join the queue , You have to think , Make a circle , Then remove each time you count to a specific , Then count !
Code
// Interview questions : Beat the drum to spread the flowers
function passGame(nameList,num){
// 1 Create a queue
let queue1 = new Queue();
// 2 Pass the values to the queue
for(i of nameList){
console.log(i);
queue1.enqueue(i);
}
// You have to cycle until there is only one person left
while(queue1.size() > 1){
// People before the number rejoin the queue
// Readers with too many arrays here may be like me , At first, I felt whether it would cross the border , Actually, subscript access is not used at all , Just keep cycling , The operation was given to queue
for(let i = 0;i<num-1;i++){
queue1.enqueue(queue1.delqueue())
}
// num The corresponding person , 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));
边栏推荐
- 12V pulse speed measurement to 24V level signal conversion transmitter
- Software architecture and design (VII) -- interactive architecture
- [live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
- 如何有效进行回顾会议(上)?
- Remember the common JS methods of projects
- 光学雨量计应用降雨量检测
- Thermistor PT100, NTC to 0-10v/4-20ma converter
- 2021 亚鸿笔试题
- Voltage to current / current to voltage module
- 管理区解耦架构见过吗?能帮客户搞定大难题的
猜你喜欢

电压转电流/电流转电压模块

Voltage to current / current to voltage module

js 链表 01

A tour of grp:05 - GRP server streaming service end stream

5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95

js 双向链表 01

One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151

分体式测斜探头安装要点及注意事项

Solve the problem that the right-click menu "edit with idle" of the 『 py 』 file is invalid or missing

热敏电阻PT100,NTC转0-10V/4-20mA转换器
随机推荐
Rust Getting Started Guide (rustup, cargo)
Pytorch - optimize model parameters
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
Samba Server Setup Guide
shell编程规范与变量
NTC,PT100热电阻转4-20mA温度信号转换器
记录一下 clearfix 清除浮动
PyQt5快速开发与实战 5.1 表格与树
Thermistor PT100, NTC to 0-10v/4-20ma converter
屏下指纹价格战再起,二线厂商今年有望拿下30%市场?
2021 肯特面试题2
开光量输入/继电器输出rs485/232远程数据采集IO模块IBF70
【直播预约】数据架构演进下的新挑战——上海站
[live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
数据实时反馈技术
5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95
2.855 billion yuan! Qingdao Xinen completed the capital increase: Xingcheng Jidian became the largest shareholder, holding 57.10%
High speed counter to rs485modbus RTU module ibf150
The price war of off screen fingerprints has resumed, and second-line manufacturers are expected to win 30% of the market this year?
Knowledge points qwer