JS优先队列排序。出队时,先找出优先级最高的元素,再按照先进先出出队。
2024-08-26 07:30:11
JS优先队列排序。出队时,先找出优先级最高的元素,再按照先进先出出队。
/*
* 优先队列
* 出队时,先找出优先级最高的元素,再按照先进先出出队。
* */
function Queue(){
this.dataStore = [];//存放队列的数组,初始化为空
this.enqueue = enqueue;//向队列尾部添加一个元素
this.dequeue = dequeue;//出队时,先找出优先级最高的元素,再按照先进先出出队。
this.theFront = theFront;//读取队首的元素
this.back = back;//对取队尾的元素
this.toStrings = toStrings;//显示队列内的所有元素
this.empty = empty;//判断队列是否为空
}
/*先定义存储队列元素的对象*/
function Patient(name,code){
this.name = name;//code是一个整数,表示患者的优先级
this.code = code;
} function enqueue(element){
this.dataStore.push(element);
} function dequeue(){
var minindex = 0;
var priority = this.dataStore[0].code;
for(var i = 1;i<this.dataStore.length;i++){
if(this.dataStore[i].code < priority){
priority = this.dataStore[i].code;
minindex = i;
}
}
return this.dataStore.splice(minindex,1);
} function theFront(){
return this.dataStore[0];
} function back(){
return this.dataStore[this.dataStore.length-1];
} function toStrings(){
return this.dataStore;
} function empty(){
if(this.dataStore.length == 0){
return true;
}else{
return false;
}
} /*优先队列的实现*/
var ed = new Queue();
var p = new Patient("aa",5);
ed.enqueue(p);
var p = new Patient("bb",4);
ed.enqueue(p);
var p = new Patient("cc",3);
ed.enqueue(p);
var p = new Patient("dd",3);
ed.enqueue(p);
var p = new Patient("ee",1);
ed.enqueue(p);
console.log(ed.toStrings());
console.log(ed.dequeue());//[ Patient { name: 'ee', code: 1 } ]
console.log(ed.dequeue());//[ Patient { name: 'cc', code: 3 } ]
console.log(ed.dequeue());//[ Patient { name: 'dd', code: 3 } ]
最新文章
- 在 CentOS7 上安装 Tomcat9
- php 遍历目录下的所以文件和文件夹
- 高版本Chrome不支持showModalDialog
- eclipse中 properties文件编码问题
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
- 在线自动下载最新版本jquery
- 小白日记18:kali渗透测试之缓冲区溢出实例(二)--Linux,穿越火线1.9.0
- 关于C#正则表达式MatchCollection类的总结,正则表达式的应用
- 代码规范-IAR设置
- 14.4.1 InnoDB Startup Configuration
- 从头开始搭建一个Spring boot+RabbitMQ环境
- 说说API的防重放机制
- kali切换字符界面模式和切换图形界面模式
- 加载Excel时,如何过滤空行空列
- 10张图带你深入理解Docker容器和镜像
- 使用Angular2的Http发送AJAX请求
- js实现获取URL参数
- centos中软件源码简单的编译安装./configure,make ,make install
- Ubuntu16.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)
- PostgreSQL 传统 hash 分区方法和性能
热门文章
- 第九章:叹词(L&#39;interjection )
- UVa 12174 Shuffle (滑动窗口)
- Ansible 笔记 (3) - 编写 playbook
- 百度上传插件 WebUploader初始使用
- 关于Python Package下的Module import方式[转]
- (连通图 模板题 出度和入度)Network of Schools--POJ--1236
- 用 PHP 编写 http 服务器
- 谷类 cereal
- WC Java 实现
- 在Windows 7上安装Team Foundation Server(TFS)的代理服务器(Agent)