javascript實現優先隊列


1.概念

    一般情況下從隊列中刪除元素,都是率先入隊的元素。但是有些使用隊列的情況不遵循先進先出的原則,這就是插隊,這需要使用優選隊列的數據結構來進行描述。

    從優先隊列中刪除元素的時候,需要考慮優先級的限制。比如醫院急診科的例子就是一個典型的優先隊列的例子。當病人進入急診室的時候,護士先根據病情給一個優先級代碼,高優先級的患者先於低優先級的患者就醫,優先級相同的根據先來先服務的順序就醫。

    定義存儲隊列元素的對象,然后構建優先隊列數據結構。    

function Patient(name, code) {
    this.name = name;
    this.code = code;
}

    變量code是一個整數,標識患者優先級或者病情驗證程度,規定優先級代碼越小優先級越高。新的dequeue() 方法遍歷隊列的底層存儲數組,從中找出優先碼最低的元素,然后使用數組的splice() 方法刪除優先級最高的元素。新的dequeue() 方法定義如下所示:

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 = i;
        }
    }
    return this.dataStore.splice(fromIndex, 1);
}

    dequeue() 方法使用簡單的順序查找方法尋找優先級最高的元素(優先碼越小優先級越高,比如,1 比5 的優先級高)。該方法返回包含一個元素的數組——從隊列中刪除的元素。

 

2.代碼實現

    完整的代碼如下所示: 

/*--------------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 = 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());

輸出結果為:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM