Skip to content

Commit

Permalink
Create Priority_queue.js (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khushal Agarwal authored and jainaman224 committed Mar 27, 2019
1 parent 433ec2d commit ac2178d
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Priority_Queue/Priority_Queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class queueElement {
constructor(element, priority) {
this.element = element;
this.prioriry = priority;
}
}

class PriorityQueue {
constructor() {
this.items = [];
}

//to add an element to the queue
enqueue(element, priority) {
var newQueueElement = new queueElement(element, priority);
var isPresent = false;
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].priority > newQueueElement.priority) {
this.items.splice(i, 0, newQueueElement);
isPresent = true;
break;
}
}
if (!isPresent) {
this.items.push(newQueueElement);
}
}

//to remove element form a queue
dequeue() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.shift();
}

//the front function
//returns the highest priority elemnt of queue
front() {
if (this.isEmpty()) {
return "Empty Queue";
}
return this.items[0];
}
//the rear function
// returns the lowest priorty element of the queue
rear() {
if (this.isEmpty()) {
return "Empty Queue";
}
return this.items[this.items.length - 1];
}
// isEmpty function
isEmpty() {
return this.items.length == 0; //true if queue is empty
}
// printQueue function

printPriorityQueue() {
var string = "";
for (var i = 0; i < this.items.length; i++)
string += this.items[i].element + " ";
return string;
}
}

var newPriorityQueue = new PriorityQueue();
newPriorityQueue.enqueue("1", 1);
newPriorityQueue.enqueue("2", 2);
newPriorityQueue.enqueue("3", 3);
newPriorityQueue.enqueue("4", 4);
newPriorityQueue.enqueue("5", 5);
console.log(newPriorityQueue.printPriorityQueue());
console.log(newPriorityQueue.front().element);
console.log(newPriorityQueue.rear().element);
console.log(newPriorityQueue.dequeue().element);
//After dequeue
console.log(newPriorityQueue.printPriorityQueue());


//OUTPUT =>

// 1 2 3 4 5
// 1
// 5
// 1
// 2 3 4 5

0 comments on commit ac2178d

Please sign in to comment.