The reason for the expansion of a limited queue is that the queues in life are usually attached with priority, such as queuing up for tickets, the general elderly and the military will have a priority limit.
Implementation: inherit the normal queue implementation of the previous Article. Here in a way, when the queue, the order is inserted into the specified position, the output is Unchanged.
Priority Queue Class
Inherit from Queuefunction priorityqueue () {queue.call (this);}
Inheriting the prototype method
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);
Add data
Add a node class function node (element, priority) {this.element = element;this.priority = priority;} Update Add PriorityQueue.prototype.enqueue = function (element, priority) {var node = new node (element, priority); 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);}}}
Full code
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)}
The following list of research links
JavaScript Data structure-priority Queue