JS优先队列排序。出队时,先找出优先级最高的元素,再按照先进先出出队。

前端之家收集整理的这篇文章主要介绍了JS优先队列排序。出队时,先找出优先级最高的元素,再按照先进先出出队。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JS优先队列排序。出队时,先找出优先级最高的元素,再按照先进先出出队。

 

502_5@502_5@502_5@ 502_5@502_5@502_5@.dataStore = [];502_5@502_5@ 502_5@.enqueue = enqueue;502_5@添加一个元素@H_502_5@ 502_5@.dequeue = dequeue;502_5@502_5@ 502_5@.theFront = theFront;502_5@502_5@ 502_5@.back = back;502_5@502_5@ 502_5@.toStrings = toStrings;502_5@显示队列内的所有元素@H_502_5@ 502_5@.empty = empty;502_5@502_5@ 502_5@502_5@502_5@502_5@ 502_5@502_5@502_5@.name = name;502_5@502_5@ 502_5@.code =@H_5025@<span style="color: #0000ff;">function@H5025@<span style="color: #000000;"> enqueue(element){
@H
5025@<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(){
@H
5025@<span style="color: #0000ff;">var@H5025@ minindex = 0<span style="color: #000000;">;
@H
5025@<span style="color: #0000ff;">var@H5025@ priority = <span style="color: #0000ff;">this@H5025@.dataStore[0<span style="color: #000000;">].code;
@H
5025@<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;">){
@H
5025@<span style="color: #0000ff;">if@H5025@(<span style="color: #0000ff;">this@H5025@.dataStore[i].code <<span style="color: #000000;"> priority){
priority @H
5025@= <span style="color: #0000ff;">this@H5025@<span style="color: #000000;">.dataStore[i].code;
minindex @H
5025@=<span style="color: #000000;"> i;
}
}
@H
5025@<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(){
@H
5025@<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(){
@H
5025@<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(){
@H
5025@<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(){
@H
5025@<span style="color: #0000ff;">if@H5025@(<span style="color: #0000ff;">this@H5025@.dataStore.length == 0<span style="color: #000000;">){
@H
5025@<span style="color: #0000ff;">return@H5025@ <span style="color: #0000ff;">true@H5025@<span style="color: #000000;">;
}@H
5025@<span style="color: #0000ff;">else@H5025@<span style="color: #000000;">{
@H
5025@<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@H
5025@ ed = <span style="color: #0000ff;">new@H5025@<span style="color: #000000;"> Queue();
@H
5025@<span style="color: #0000ff;">var@H5025@ p = <span style="color: #0000ff;">new@H5025@ Patient("aa",5<span style="color: #000000;">);
ed.enqueue(p);
@H
5025@<span style="color: #0000ff;">var@H5025@ p = <span style="color: #0000ff;">new@H5025@ Patient("bb",4<span style="color: #000000;">);
ed.enqueue(p);
@H
5025@<span style="color: #0000ff;">var@H5025@ p = <span style="color: #0000ff;">new@H5025@ Patient("cc",3<span style="color: #000000;">);
ed.enqueue(p);
@H
5025@<span style="color: #0000ff;">var@H5025@ p = <span style="color: #0000ff;">new@H5025@ Patient("dd",3<span style="color: #000000;">);
ed.enqueue(p);
@H
5025@<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());@H
5025@<span style="color: #008000;">//@H5025@<span style="color: #008000;">[ Patient { name: 'ee',code: 1 } ]@H5025@
console.log(ed.dequeue());<span style="color: #008000;">//@H
5025@<span style="color: #008000;">[ Patient { name: 'cc',code: 3 } ]@H5025@
console.log(ed.dequeue());<span style="color: #008000;">//@H
5025@<span style="color: #008000;">[ Patient { name: 'dd',code: 3 } ]@H502_5@

 

猜你在找的JavaScript相关文章