-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.js
50 lines (47 loc) · 1.57 KB
/
PriorityQueue.js
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
const Queue = require('../../ED/src/Queue');
const PriorityQueue = (()=>{
// const queue = new WeakMap();
class PriorityQueue extends Queue{
constructor(type='max'){
super();
if(type.toLowerCase() !== 'max' && type.toLowerCase() !== 'min'){
type = 'max';
}
this.type = type.toLowerCase();//nao funciona variaveis privadas
// queue.set(this,[]);
}
getType(){
return this.type;
}
enqueue(item, priority=1){
let queueElement ={
item: item,
priority: priority
};
let added = false;
for(let i=0; i < super.get().length; i++){
if(!added && this.type === 'max'){// verificar
if(queueElement.priority > (super.get())[i].priority){
super.get().splice(i, 0, queueElement);
added = true;
break;
}
} else if(!added && this.type === 'min'){
if(queueElement.priority < (super.get())[i].priority){
super.get().splice(i, 0, queueElement);
added = true;
break;
}
}
}
if(!added){
(super.get()).push(queueElement);
}
}
// get(){
// }
}
return PriorityQueue;
})();
exports.default = PriorityQueue;
module.exports = exports['default'];