js优先队列

/*--------------Queue类的定义和测试代码----------------*/
function Queue(){
        this.dataStore = [];
        this.enqueue = enqueue;
        this.dequeue = dequeue;
        this.front = front;
        this.back = back;
        this.toString = toString;
        this.empty = empty;
}

//入队,就是在数组的末尾添加一个元素
function enqueue(element){
    this.dataStore.push(element);
}
//出队,判断优先级删除,注意这里用的是数组的splice方法,不是slice方法
function dequeue(){
    var priority = this.dataStore[0].code;
    var fromIndex = 0;
    for (var i=1; i<this.dataStore.length; ++i) {
        if (this.dataStore[i].code < priority) {     //都是同第一个值进行比较,所以fromIndex=4,所以取出的值是最后一个
            fromIndex = i;
        }

    }
    return this.dataStore.splice(fromIndex, 1);
}
//取出数组的第一个元素
function front(){
    return this.dataStore[0];
}
//取出数组的最后一个元素
function back(){
    return this.dataStore[this.dataStore.length-1];
}

function toString(){
    var retStr = "";
    for (var i=0; i<this.dataStore.length; ++i) {
        retStr += "病人:" + this.dataStore[i].name + " 优先级:" + this.dataStore[i].code + "<br>"
    }
    return retStr;
}
//判断数组是否为空
function empty(){
    if(this.dataStore.length == 0){
        return true;
    }else{
        return false;
    }    
}
//返回数组中元素的个数
function count(){
    return this.dataStore.length;
}


/*----------------基数排序-----------------*/

function Patient(name, code){
    this.name = name;
    this.code = code;
}
var p = new Patient('smith', 5);
var ed = new Queue();
ed.enqueue(p);


p = new Patient('jones', 4);
ed.enqueue(p);


p = new Patient('fehrendbach', 6);
ed.enqueue(p);


p = new Patient('brown', 1);
ed.enqueue(p);


p = new Patient('ingram', 1);
ed.enqueue(p);


document.write(ed.toString());


var seen = ed.dequeue();
document.write('<br>');
document.write("服务病人:" + seen[0].name);


document.write('<br>');
document.write(ed.toString());


seen = ed.dequeue();
document.write('<br>');
document.write("服务病人:" + seen[0].name);


document.write('<br>');
document.write(ed.toString());




seen = ed.dequeue();
document.write('<br>');
document.write("服务病人:" + seen[0].name);


document.write('<br>');

document.write(ed.toString());

——————————————————————————————————————————————————

打印出:

病人:smith 优先级:5
病人:jones 优先级:4
病人:fehrendbach 优先级:6
病人:brown 优先级:1
病人:ingram 优先级:1

服务病人:ingram
病人:smith 优先级:5
病人:jones 优先级:4
病人:fehrendbach 优先级:6
病人:brown 优先级:1

服务病人:brown
病人:smith 优先级:5
病人:jones 优先级:4
病人:fehrendbach 优先级:6

服务病人:jones
病人:smith 优先级:5

病人:fehrendbach 优先级:6

————————————————————————————————————————————————————

第二个例子:

/*--------------Queue类的定义和测试代码----------------*/
function Queue(){
        this.dataStore = [];
        this.enqueue = enqueue;
        this.dequeue = dequeue;
        this.front = front;
        this.back = back;
        this.toString = toString;
        this.empty = empty;
}


//入队,就是在数组的末尾添加一个元素
function enqueue(element){
    this.dataStore.push(element);
}
//出队,判断优先级删除,注意这里用的是数组的splice方法,不是slice方法
function dequeue(){
    var fromIndex = 0;
    for (var i=1; i<this.dataStore.length; ++i) {
debugger;
        if (this.dataStore[i].code < this.dataStore[fromIndex].code) {
            fromIndex = i;
        }
    }
    return this.dataStore.splice(fromIndex, 1);
}
//取出数组的第一个元素
function front(){
    return this.dataStore[0];
}
//取出数组的最后一个元素
function back(){
    return this.dataStore[this.dataStore.length-1];
}


function toString(){
    var retStr = "";
    for (var i=0; i<this.dataStore.length; ++i) {
        retStr += "病人:" + this.dataStore[i].name + " 优先级:" + this.dataStore[i].code + "<br>"
    }
    return retStr;
}
//判断数组是否为空
function empty(){
    if(this.dataStore.length == 0){
        return true;
    }else{
        return false;
    }    
}
//返回数组中元素的个数
function count(){
    return this.dataStore.length;
}


/*----------------基数排序-----------------*/




function Patient(name, code){
    this.name = name;
    this.code = code;
}
var p = new Patient('smith', 5);
var ed = new Queue();
ed.enqueue(p);


p = new Patient('jones', 4);
ed.enqueue(p);


p = new Patient('fehrendbach', 6);
ed.enqueue(p);


p = new Patient('brown', 1);
ed.enqueue(p);


p = new Patient('ingram', 1);
ed.enqueue(p);


document.write(ed.toString());


var seen = ed.dequeue();
document.write('<br>');
document.write("服务病人:" + seen[0].name);


document.write('<br>');
document.write(ed.toString());


seen = ed.dequeue();
document.write('<br>');
document.write("服务病人:" + seen[0].name);


document.write('<br>');
document.write(ed.toString());




seen = ed.dequeue();
document.write('<br>');
document.write("服务病人:" + seen[0].name);


document.write('<br>');

document.write(ed.toString());

————————————————————

打印:

病人:smith 优先级:5
病人:jones 优先级:4
病人:fehrendbach 优先级:6
病人:brown 优先级:1
病人:ingram 优先级:1

服务病人:brown
病人:smith 优先级:5
病人:jones 优先级:4
病人:fehrendbach 优先级:6
病人:ingram 优先级:1

服务病人:ingram
病人:smith 优先级:5
病人:jones 优先级:4
病人:fehrendbach 优先级:6

服务病人:jones
病人:smith 优先级:5
病人:fehrendbach 优先级:6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值