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