-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (116 loc) · 2.94 KB
/
Copy pathindex.js
File metadata and controls
136 lines (116 loc) · 2.94 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
export default class PriorityQueue {
/**
* initializes the queue
* @param {number} capacity - the capacity of the queue
*/
constructor(capacity = Infinity) {
this._capacity = capacity;
this._data = [];
this._front = 0;
this._rear = 0;
this._priorities = {};
}
/**
* adds a new element onto the queue
* @param {any} element
* @param {number} priority
* @return {number|string} the index of the added element or queue overflow message
*/
enqueue(element, priority) {
let enqueued = false;
if (this.size() < this._capacity) {
this._rear++;
this._data.forEach((item, index) => {
if (this._priorities[item] > priority) {
this._data.splice(index, 0, element);
enqueued = true;
}
});
if (!enqueued) {
this._data.push(element);
}
this._priorities[element] = priority;
return this._rear;
}
return 'Max capacity reached. Remove element before adding a new one.';
}
/**
* does the reverse of the enqueue() i.e. removes an element
* from the queue and then increments the front pointer variable
* @return {any} the element in the front position of the queue or an error message
*/
dequeue() {
if (this._front === this._rear) {
return 'No element inside the queue. Add element before dequeuing.';
}
const element = this._data.shift();
delete this._priorities[element];
if (this._front < this._rear) this._front++;
return element;
}
/**
* returns the oldest element added onto the queue. similar to dequeue but
* does not remove element from queue
* @return {any} returns the oldest element in the queue
*/
peek() {
return this._data[this._front];
}
/**
* examine the front element of the queue
* @return {any} the element in the front position of the queue
*/
front() {
return this._data[this._front];
}
/**
* examine the back element of the queue
* @return {any} the element in the rear position of the queue
*/
back() {
return this._data[this._rear - 1];
}
/**
* returns the size/length of the queue
* @return {number} the difference of the front and rear values
*/
size() {
return this._rear - this._front;
}
/**
* check whether the queue is empty
* @return {boolean}
*/
isEmpty() {
return this._rear - 1 - this._front === 0;
}
/**
* clears the queue and sets the top pointer variable back to 0
*/
clear() {
this._rear = 0;
this._front = 0;
this._data = [];
}
/**
* displays the queue
*/
print() {
return this._data.join(' ');
}
/**
* returns the set capacity of the stack
* @return {number}
*/
capacity() {
return this._capacity;
}
/**
* check wheteher an element exists in the queue
* @param {any} element the element to search
* @return {boolean}
*/
contains(element) {
return this._data.includes(element);
}
}