Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/varun/priority queue #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions data-structures-in-javascript/priority-queue.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
class Node {
constructor(data, priority) {
this.data = data;
this.priority = priority
}
}

/**
* Priority Queue Class here implements the data structure 'Priority Queue' using javascript array.
* Element with priority of smaller number are lined up 1st i.e. they are given higher priority.
* If for 2 elements they have same priority then in that case we serve it as FCFS i.e. new element is inserted
* at back of the already inserted element with same priority.
*/
class PriorityQueue {
constructor() {
this.queue = [];
}

/**
* @description It creates a new Node with (data and priority) and inserts it into queue based on priority.
* Priority with lowest number is at higher priority.
* @param {*} data Element which is to be inserted.
* @param {number} priority Priority of the element it be inserted.
*/
insert(data, priority) {
// finding the position where to insert the data
let index = 0;
for (; index < this.size; index++) {
if (priority < this.queue[index].priority) {
break;
}
}
// creating new node and inserts data
let newNode = new Node(data, priority);
this.queue.splice(index, 0, newNode);
}

/**
* @description It removes the data of highest priority
*/
remove() {
return this.queue.shift();
}

/**
* @description It returns the length of the queue
*/
length() {
return this.queue.length;
}

/**
* @description It checks if the queue is empty
*/
isEmpty() {
return this.queue.length === 0;
}

/**
* @description It iterates over the elements of queue
* @param {*} fn
*/
iterator(fn) {
for (let node of this.queue) {
fn(node);
}
}

/**
* @description It prints the queue
*/
print() {
for (let node of this.queue) {
console.log('data:', node.data, 'priority:', node.priority);
}
}
}





let pQueue = new PriorityQueue();
console.log(pQueue.isEmpty());
pQueue.insert(12, 5);
console.log(pQueue.isEmpty());
pQueue.insert(55, 2);
console.log(pQueue.length());
pQueue.insert(23, 5);
pQueue.insert(134, 8);
pQueue.print();
console.log(pQueue.remove());
console.log(pQueue.length());
console.log(pQueue.remove());
console.log(pQueue.length());
pQueue.insert(194, 11);
pQueue.print();
console.log(pQueue.iterator);

pQueue.iterator(item => {
console.log(item);
});
98 changes: 98 additions & 0 deletions data-structures-in-javascript/priority-queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
function Node(data, priority) {
this.data = data;
this.priority = priority
}

/**
* Priority Queue Class here implements the data structure 'Priority Queue' using javascript array.
* Element with priority of smaller number are lined up 1st i.e. they are given higher priority.
* If for 2 elements they have same priority then in that case we serve it as FCFS i.e. new element is inserted
* at back of the already inserted element with same priority.
*/
function PriorityQueue() {
this.queue = [];
}

/**
* @description It creates a new Node with (data and priority) and inserts it into queue based on priority.
* Priority with lowest number is at higher priority.
* @param {*} data Element which is to be inserted.
* @param {number} priority Priority of the element it be inserted.
*/
PriorityQueue.prototype.insert = function (data, priority) {
// finding the position where to insert the data
let index = 0;
for (; index < this.size; index++) {
if (priority < this.queue[index].priority) {
break;
}
}
// creating new node and inserts data
let newNode = new Node(data, priority);
this.queue.splice(index, 0, newNode);
}

/**
* @description It removes the data of highest priority
*/
PriorityQueue.prototype.remove = function () {
return this.queue.shift();
}

/**
* @description It returns the length of the queue
*/
PriorityQueue.prototype.length = function () {
return this.queue.length;
}

/**
* @description It checks if the queue is empty
*/
PriorityQueue.prototype.isEmpty = function () {
return this.queue.length === 0;
}

/**
* @description It iterates over the elements of queue
* @param {*} fn
*/
PriorityQueue.prototype.iterator = function (fn) {
for (let node of this.queue) {
fn(node);
}
}

/**
* @description It prints the queue
*/
PriorityQueue.prototype.print = function () {
for (let node of this.queue) {
console.log('data:', node.data, 'priority:', node.priority);
}
}





let pQueue = new PriorityQueue();
console.log(pQueue.isEmpty());
pQueue.insert(12, 5);
console.log(pQueue.isEmpty());
pQueue.insert(55, 2);
console.log(pQueue.length());
pQueue.insert(23, 5);
pQueue.insert(134, 8);
pQueue.print();
console.log(pQueue.remove());
console.log(pQueue.length());
console.log(pQueue.remove());
console.log(pQueue.length());
pQueue.insert(194, 11);
pQueue.print();
console.log(pQueue.iterator);

pQueue.iterator(item => {
console.log(item);
});