Created
November 21, 2017 11:29
-
-
Save mjarpitanand/9783507c136462b426e20fdee548fad7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function priorityQueue(){ | |
var items = []; //main array for the queue | |
function queueElement(item , pos){ | |
this.item = item; | |
this.pos = pos; | |
} | |
var added = false; | |
this.enque = function(item , pos){ | |
var q = new queueElement(item , pos); | |
for(var i=0; i<items.length ; i++){ | |
if(q.pos < items[i].pro){ | |
items.splice(i , 0 , q); | |
added = true; | |
break; | |
} | |
if(addded !== true){ | |
items.push(q); | |
} | |
} | |
this.print = function(){ | |
items.forEach(function(x){ | |
console.log(x); | |
}) | |
} | |
} | |
var pq = new priorityQueue(); | |
pq.enque(1 , 1); | |
pq.enque(1 , 2); | |
pq.enque(1 , 3); | |
pq.print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment