JS优先队列排序。出队时,先找出优先级最高的元素,再按照先进先出出队。
<span style="color: #0000ff;">this<span style="color: #000000;">.dataStore.push(element);
} <span style="color: #0000ff;">function<span style="color: #000000;"> dequeue(){
<span style="color: #0000ff;">var minindex = 0<span style="color: #000000;">;
<span style="color: #0000ff;">var priority = <span style="color: #0000ff;">this.dataStore[0<span style="color: #000000;">].code;
<span style="color: #0000ff;">for(<span style="color: #0000ff;">var i = 1;i<<span style="color: #0000ff;">this.dataStore.length;i++<span style="color: #000000;">){
<span style="color: #0000ff;">if(<span style="color: #0000ff;">this.dataStore[i].code <<span style="color: #000000;"> priority){
priority = <span style="color: #0000ff;">this<span style="color: #000000;">.dataStore[i].code;
minindex =<span style="color: #000000;"> i;
}
}
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore.splice(minindex,1<span style="color: #000000;">);
} <span style="color: #0000ff;">function<span style="color: #000000;"> theFront(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[0<span style="color: #000000;">];
} <span style="color: #0000ff;">function<span style="color: #000000;"> back(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.dataStore.length-1<span style="color: #000000;">];
}
<span style="color: #0000ff;">function<span style="color: #000000;"> toStrings(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this<span style="color: #000000;">.dataStore;
}
<span style="color: #0000ff;">function<span style="color: #000000;"> empty(){
<span style="color: #0000ff;">if(<span style="color: #0000ff;">this.dataStore.length == 0<span style="color: #000000;">){
<span style="color: #0000ff;">return <span style="color: #0000ff;">true<span style="color: #000000;">;
}<span style="color: #0000ff;">else<span style="color: #000000;">{
<span style="color: #0000ff;">return <span style="color: #0000ff;">false<span style="color: #000000;">;
}
}
<span style="color: #008000;">/<span style="color: #008000;">优先队列的实现<span style="color: #008000;">/
<span style="color: #0000ff;">var ed = <span style="color: #0000ff;">new<span style="color: #000000;"> Queue();
<span style="color: #0000ff;">var p = <span style="color: #0000ff;">new Patient("aa",5<span style="color: #000000;">);
ed.enqueue(p);
<span style="color: #0000ff;">var p = <span style="color: #0000ff;">new Patient("bb",4<span style="color: #000000;">);
ed.enqueue(p);
<span style="color: #0000ff;">var p = <span style="color: #0000ff;">new Patient("cc",3<span style="color: #000000;">);
ed.enqueue(p);
<span style="color: #0000ff;">var p = <span style="color: #0000ff;">new Patient("dd",3<span style="color: #000000;">);
ed.enqueue(p);
<span style="color: #0000ff;">var p = <span style="color: #0000ff;">new Patient("ee",1<span style="color: #000000;">);
ed.enqueue(p);
console.log(ed.toStrings());
console.log(ed.dequeue());<span style="color: #008000;">//<span style="color: #008000;">[ Patient { name: 'ee',code: 1 } ]
console.log(ed.dequeue());<span style="color: #008000;">//<span style="color: #008000;">[ Patient { name: 'cc',code: 3 } ]
console.log(ed.dequeue());<span style="color: #008000;">//<span style="color: #008000;">[ Patient { name: 'dd',code: 3 } ]
原文链接:https://www.f2er.com/js/403381.html