-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.hpp
63 lines (59 loc) · 1.39 KB
/
PriorityQueue.hpp
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
#ifndef PriorityQueue_h
#define PriorityQueue_h
#include <cstdlib>
#include "PQNode.hpp"
template<typename T>
class PriorityQueue{
private:
size_t count;
PQNode<T>* first;
public:
PriorityQueue(){
count = 0;
first = nullptr;
}
~PriorityQueue() {
while (first)
pop();
}
inline T front() {
return(first) ? first->data : NULL;
}
inline size_t frontPriority(){
return first->priority;
}
void pop() {
if (first) {
PQNode<T>* toDelete = first;
first = first->next;
delete toDelete;
count--;
}
}
void push(T data, size_t priority){
if(first){
PQNode<T>* runner = first;
PQNode<T>* noeud = new PQNode<T>(data, priority);
if(noeud->priority < first->priority){
noeud->next = first;
first = noeud;
count ++;
}
else{
while(runner->next && priority >= runner->next->priority)
runner = runner->next;
noeud->next = runner->next;
runner->next = noeud;
count ++;
}
}
else{
first = new PQNode<T>(data, priority);
count ++;
}
}
inline size_t size(){
return count;
}
};
#endif