当前位置:网站首页>js 队列
js 队列
2022-07-28 14:51:00 【PBitW】
队列

队列结构

队列的创建 – 数组方法

代码实现
<!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>封装队列</title>
</head>
<body>
<script> function Queue(){
// 属性 this.items = []; // 方法 // 1.将元素加入到队列 Queue.prototype.enqueue = function(element){
this.items.push(element); }; // 2.从队列中删除前端元素 Queue.prototype.delqueue = function(element){
// 记住这里得有返回值不然后面击鼓传花是undefined,因为函数默认返回值是undefined return this.items.shift(element); }; // 3.查看前端元素 Queue.prototype.front = function(){
return this.items[0]; } // 4.查看队列是否为空 Queue.prototype.isEmpty = function(){
return this.items.length == 0; } // 5.查看队列中元素个数 Queue.prototype.size = function(){
return this.items.length; } // 6.toString方法 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>
队列的实际运用 – 击鼓传花
题目:
这里其实并不好思考到和队列搭上边,建议看代码前,读者先仔细想想,自己做的时候会怎么做!
想和队列搭上边,就得思考到,围成一个圈,然后每次数到特定的就移除,然后接着数!
代码
// 面试题:击鼓传花
function passGame(nameList,num){
// 1 创建一个队列
let queue1 = new Queue();
// 2 将值都传给队列
for(i of nameList){
console.log(i);
queue1.enqueue(i);
}
// 得一直循环到只剩一个人
while(queue1.size() > 1){
// 数字之前的人重新加入到队列
//这里数组做多了的读者可能会和我一样,一开始感觉会不会越界,其实这个根本没用到下标访问,只是一直循环,操作交给了queue
for(let i = 0;i<num-1;i++){
queue1.enqueue(queue1.delqueue())
}
// num对应的人,直接删除
queue1.delqueue();
}
console.log(queue1.front());
return nameList.indexOf(queue1.front());
}
let arr = ["1","2","3","4","5"];
console.log(passGame(arr,3));
边栏推荐
- 记:数字累加动画
- 高速计数器转RS485Modbus RTU模块IBF150
- 多用型混合信号8AI/4DI/DO转串口RS485/232MODBUS采集模块IBF30
- How to obtain and embed go binary execution package information
- Set static IP in NAT mode of virtual machine
- 生命的感悟
- Samba Server Setup Guide
- Huawei has a record number of employees worldwide: 194000, with research and development personnel accounting for nearly 50%
- Software architecture and design (VIII) -- distributed architecture
- How to quickly access the unified authentication system
猜你喜欢
随机推荐
Youdao cloud notes remove the bottom advertisement
Try... Except exception handling statement (6)
以太网转RS485串口计数器WiFI模块 LED灯光控制器IBF165
How to configure Samba server
Data real-time feedback technology
Shell programming specifications and variables
MLX90640 红外热成像仪测温传感器模块开发笔记(八)
Knowledge points qwer
Rxdart is used instead of stateful in fluent
有道云笔记去除底部广告
Minimum heap improves the efficiency of each sort
Docker implements redis cluster mode hash slot partition for 100 million level data storage
1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151
Software architecture and design (x) -- Architecture Technology
在OBS上进行H265推流
Software architecture and design (VI) -- hierarchy
Samba Server Setup Guide
软件架构与设计(四)-----数据流架构
使用systemd管理服务
Has won Huawei's 8.5 billion yuan screen order? Vicino responded: the customer asked for confidentiality and could not reply!









