-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cpp
More file actions
64 lines (49 loc) · 1.08 KB
/
PriorityQueue.cpp
File metadata and controls
64 lines (49 loc) · 1.08 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
#ifndef PRIORITYQUEUE_CPP_INCLUDED
#define PRIORITYQUEUE_CPP_INCLUDED
#include "PriorityQueue.h"
template <class T>
PriorityQueue<T>::PriorityQueue()
{
NoOfElements = 0;
}
template <class T>
PriorityQueue<T>::~PriorityQueue()
{
// Do nothing
}
template <class T>
void PriorityQueue<T>::insert(int key, T value)
{
data[++NoOfElements].key = key;
data[NoOfElements].value = value;
int child = NoOfElements, parent = child / 2;
while (parent > 0 && data[parent].value > data[child].value)
{
swap(data[parent], data[child]);
child = parent;
parent = child / 2;
}
}
template <class T>
int PriorityQueue<T>::remove()
{
int return_value = data[1].key;
data[1] = data[NoOfElements--];
int parent = 1, child = 2 * parent;
while (child <= NoOfElements)
{
if (data[child + 1].value < data[child].value)
child++;
if (data[parent].value > data[child].value)
swap(data[parent], data[child]);
parent = child;
child = 2 * parent;
}
return return_value;
}
template <class T>
bool PriorityQueue<T>::isEmpty()
{
return (NoOfElements == 0);
}
#endif