当前位置:网站首页>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>
总结
这里总结一下,现在写的不管是堆还是队列,都是通过数组实现的,而数组又提供了很多方便的操作,所以基本上都是数组方法的调用,所以理解起来很简单,写起来也很简单!但是这里要理解的也并不是数组的操作,而是如何定义数据结构,使其能实现堆或者列表!
下一篇才算是真正的步入数据结构!
边栏推荐
- A wave of operation to solve the error problem of Laya scene editor
- Has won Huawei's 8.5 billion yuan screen order? Vicino responded: the customer asked for confidentiality and could not reply!
- How to turn on and off flight mode through ADB
- 软件架构与设计(八)-----分布式架构
- Using SYSTEMd to manage services
- 生命的感悟
- PXE网络装机
- 便携式钻孔测斜仪数据采集仪测量原理与测斜探头的连接及使用方法
- 已拿下华为85亿元屏幕订单?维信诺回应:客户要求保密,无法回复!
- Give you a linked list, delete the penultimate node of the linked list, and return the head node of the linked list.
猜你喜欢

如何有效进行回顾会议(上)?

软件架构与设计(八)-----分布式架构

MLX90640 红外热成像仪测温传感器模块开发笔记(八)

Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!

DNS域名解析协议

PXE network installation

如何快速接入统一的认证鉴权体系

Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module

多用型混合信号8AI/4DI/DO转串口RS485/232MODBUS采集模块IBF30

生命的感悟
随机推荐
软件架构与设计(九)-----基于组件的架构
电压频率的变换原理
How to compress and decompress ramdisk.img
Framework定制系列(一)-----SystemUI NavigationBar导航栏上滑返回Launcher
有道云笔记去除底部广告
数牍 X Rust,那些不得不说的事
Rust 入门指南(rustup, cargo)
软件架构与设计(十)-----架构技术
记:数字累加动画
5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95
Pyqt5 rapid development and practice 5.2 container: load more controls
Problem of fetching combinatorial numbers
使用systemd管理服务
Youdao cloud notes remove the bottom advertisement
Minimum heap improves the efficiency of each sort
射频模块无线收发RF63U芯片应用数据传输和基建网络
记录一下 clearfix 清除浮动
Has won Huawei's 8.5 billion yuan screen order? Vicino responded: the customer asked for confidentiality and could not reply!
What is the concept of game testing? What are the test methods and processes?
为赴美上市做准备?Arm宣布将剥离物联网服务业务:未来将聚焦芯片底层设计