By
donglegend
2016-11-07
更新日期:2024-04-24
这里之所以扩充一个 有限队列 是因为,生活使用中队列通常会附加优先级,比如排队买票,一般老人和军人等会有优先权限。
实现:继承上篇的 普通队列实现。这里用一种方法,入队的时候,进行排序插入到指定位置,输出不变。
优先队列类 1 2 3 4 function PriorityQueue () { Queue.call(this ); }
继承原型方法 1 2 3 4 5 6 7 8 9 10 11 12 function base (p, c ) { var h = {}, P = p.prototype, C = c.prototype; for (var k in C){ h[k] = 1 ; } for (var k in P){ if (!h[k]){ C[k] = P[k]; } } } base(Queue, PriorityQueue);
添加数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function Node(element, priority){ this .element = element; this .priority = priority; } PriorityQueue.prototype.enqueue = function (element, priority){ var node = new Node(element, priority); if (this .isEmpty()){ this .data .push(node); }else { var add = false ; for (var i = 0 ,len=this .data .length; i<len; i++){ if (node.priority < this .data [i].priority){ this .data .splice(i, 0 , node); add = true ; break ; } } if (!add){ this .data .push(node); } } }
更新print方法 1 2 3 PriorityQueue.prototype.print = function ( ) { console .dir(this .data) }
完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 function PriorityQueue ( ) { Queue.call(this ); } function base (p, c ) { var h = {}, P = p.prototype, C = c.prototype; for (var k in C){ h[k] = 1 ; } for (var k in P){ if (!h[k]){ C[k] = P[k]; } } } base(Queue, PriorityQueue); function Node (element, priority ) { this .element = element; this .priority = priority; } PriorityQueue.prototype.enqueue = function (element, priority ) { var node = new Node(element, priority); if (this .isEmpty()){ this .data.push(node); }else { var add = false ; for (var i = 0 ,len=this .data.length; i<len; i++){ if (node.priority < this .data[i].priority){ this .data.splice(i, 0 , node); add = true ; break ; } } if (!add){ this .data.push(node); } } } PriorityQueue.prototype.print = function ( ) { console .dir(this .data) }
下面研究链表