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

例子
最常见的例子就是操作系统中的线程,重要的先执行不重要的后执行(当然电脑肯定不是按照接下来的代码实现的,只是操作过程类似)
优先级队列实现 – 数组方法
和队列一样,只是添加元素需要考虑插入地点,以及转变为字符串需要改变一下输出方式,不然会变成很多object !
代码:
<!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>Document</title>
</head>
<body>
<script> function PriorityQueue(){
// 封装类 --> 在PriorityQueue里面重新封创建了一个类:可以理解为内部类 function QueueElement(element,priority){
this.element = element; this.priority = priority; } // 属性 this.items = []; // 实现插入方法 PriorityQueue.prototype.enqueue = function(element,priority){
let queueElement = new QueueElement(element,priority); if(this.items.length === 0){
this.items.push(queueElement); }else{
let added = false; for(let i = 0;i<this.items.length;i++){
if(queueElement.priority < this.items[i].priority){
this.items.splice(i,0,queueElement); added = true; break; } } if(added == false){
this.items.push(queueElement); } } } // 2.从队列中删除前端元素 PriorityQueue.prototype.delqueue = function(element){
return this.items.shift(element); }; // 3.查看前端元素 PriorityQueue.prototype.front = function(){
return this.items[0]; } // 4.查看队列是否为空 PriorityQueue.prototype.isEmpty = function(){
return this.items.length == 0; } // 5.查看队列中元素个数 PriorityQueue.prototype.size = function(){
return this.items.length; } // 6.toString方法 PriorityQueue.prototype.toString = function(){
let a = []; for(let i =0;i < this.items.length;i++){
a[i] = this.items[i].element + "-" + this.items[i].priority } return a.join(" "); } } var pq = new PriorityQueue(); pq.enqueue('abc',111); pq.enqueue('abd',11); pq.enqueue('nbc',50); pq.enqueue('dfg',90); pq.enqueue('bvn',3); console.log(pq); pq.delqueue(); console.log(pq.toString()); </script>
</body>
</html>
总结
这里总结一下,现在写的不管是堆还是队列,都是通过数组实现的,而数组又提供了很多方便的操作,所以基本上都是数组方法的调用,所以理解起来很简单,写起来也很简单!但是这里要理解的也并不是数组的操作,而是如何定义数据结构,使其能实现堆或者列表!
下一篇才算是真正的步入数据结构!
边栏推荐
- PXE network installation
- Love programming
- Pyqt5 rapid development and practice 5.1 tables and trees
- Shell编程规范与变量
- 电压频率的变换原理
- Leetcode bracket validity problem
- 光学雨量计应用降雨量检测
- Has won Huawei's 8.5 billion yuan screen order? Vicino responded: the customer asked for confidentiality and could not reply!
- 有道云笔记去除底部广告
- 记录一下 clearfix 清除浮动
猜你喜欢

Duty cycle switch output high speed pulse counter rtumodbus module ibf63

Docker implements redis cluster mode hash slot partition for 100 million level data storage

The advanced path of programmers

Docker容器实现MySQL主从复制

Multipurpose mixed signal 8ai/4di/do to serial port rs485/232modbus acquisition module ibf30

低成本/小体积模块RS485/232转模拟信号的原理以及应用IBF33
What are the process specifications of software testing? How to do it?

shell编程规范与变量

5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95

FTP文件传输协议
随机推荐
Framework定制系列(六)-----屏蔽FallbackHome手机启动中弹窗直接进入Launcher
使用systemd管理服务
Multipurpose mixed signal 8ai/4di/do to serial port rs485/232modbus acquisition module ibf30
Proportional solenoid valve control valve 4-20mA to 0-165ma/330ma signal isolation amplifier
Knowledge points qwer
Software architecture and design (VII) -- interactive architecture
以太网转RS485串口计数器WiFI模块 LED灯光控制器IBF165
1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151
Software architecture and design (V) -- data centric architecture
How to configure Samba server
MLX90640 红外热成像仪测温传感器模块开发笔记(八)
Zhaoqi scientific innovation and entrepreneurship competition platform, activity roadshow, investment and financing docking
Software architecture and design (IX) -- component based architecture
Framework customization series (10) -- systemui customization status bar statusbar and navigation bar tutorial
Rust 入门指南(rustup, cargo)
How to turn on and off flight mode through ADB
Camera连拍自动化测试shell脚本
Huawei has a record number of employees worldwide: 194000, with research and development personnel accounting for nearly 50%
Software architecture and design (x) -- Architecture Technology
语音社交系统——完善有声系统产业链