-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.js
More file actions
121 lines (105 loc) · 3.42 KB
/
priorityqueue.js
File metadata and controls
121 lines (105 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//https://www.geeksforgeeks.org/implementation-priority-queue-javascript/amp/
// User defined class
// to store element and its priority
class QElement {
constructor(element, priority) {
this.element = element;
this.priority = priority;
}
}
// PriorityQueue class
class PriorityQueue {
// An array is used to implement priority
constructor() {
this.items = [];
}
// enqueue function to add element
// to the queue as per priority
enqueue(element, priority) {
// creating object from queue element
var qElement = new QElement(element, priority);
var contain = false;
// iterating through the entire
// item array to add element at the
// correct location of the Queue
for (var n = 0; n < this.items.length; n++) {
if (this.items[n].priority > qElement.priority) {
// Once the correct location is found it is
// enqueued
this.items.splice(n, 0, qElement);
contain = true;
break;
}
}
// if the element have the highest priority
// it is added at the end of the queue
if (!contain) {
this.items.push(qElement);
}
}
// dequeue method to remove
// element from the queue
dequeue() {
// return the dequeued element
// and remove it.
// if the queue is empty
// returns Underflow
if (this.isEmpty()) return "Underflow";
return this.items.shift();
}
// front function
front() {
// returns the highest priority element
// in the Priority queue without removing it.
if (this.isEmpty()) return "No elements in Queue";
return this.items[0];
}
// rear function
rear() {
// returns the lowest priorty
// element of the queue
if (this.isEmpty()) return "No elements in Queue";
return this.items[this.items.length - 1];
}
// isEmpty function
isEmpty() {
// return true if the queue is empty.
return this.items.length == 0;
}
// printQueue function
// prints all the element of the queue
printPQueue() {
var str = "";
for (var n = 0; n < this.items.length; n++)
str += this.items[n].element + " ";
return str;
}
}
//---------------
// creating object for queue classs
var priorityQueue = new PriorityQueue();
// testing isEmpty and front on an empty queue
// return true
console.log(priorityQueue.isEmpty());
// returns "No elements in Queue"
console.log(priorityQueue.front());
// adding elements to the queue
priorityQueue.enqueue("Sumit", 2);
priorityQueue.enqueue("Gourav", 1);
priorityQueue.enqueue("Piyush", 1);
priorityQueue.enqueue("Sunny", 2);
priorityQueue.enqueue("Sheru", 3);
// prints [Gourav Piyush Sumit Sunny Sheru]
console.log(priorityQueue.printPQueue());
// prints Gourav
console.log(priorityQueue.front().element);
// pritns Sheru
console.log(priorityQueue.rear().element);
// removes Gouurav
// priorityQueue contains
// [Piyush Sumit Sunny Sheru]
console.log(priorityQueue.dequeue().element);
// Adding another element to the queue
priorityQueue.enqueue("Sunil", 2);
// prints [Piyush Sumit Sunny Sunil Sheru]
console.log(priorityQueue.printPQueue());