Skip to content

Instantly share code, notes, and snippets.

@mjarpitanand
Created November 21, 2017 11:29
Show Gist options
  • Save mjarpitanand/9783507c136462b426e20fdee548fad7 to your computer and use it in GitHub Desktop.
Save mjarpitanand/9783507c136462b426e20fdee548fad7 to your computer and use it in GitHub Desktop.
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