This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.h
170 lines (137 loc) · 3.73 KB
/
PriorityQueue.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Created by hartings on 04.06.18.
//
#ifndef OOA4_PRIORITYQUEUE_H
#define OOA4_PRIORITYQUEUE_H
#include <iostream>
#include "Execption.h"
using namespace std;
template<typename T>
class PriorityQueueEntry {
public:
float priority;
T value;
PriorityQueueEntry<T> *next, *prev;
PriorityQueueEntry(T value, float priority) {
this->value = value;
this->priority = priority;
next = nullptr;
prev = nullptr;
}
};
template<typename T>
class PriorityQueue {
private:
int _size;
PriorityQueueEntry<T> *_head;
PriorityQueueEntry<T> *_tail;
public:
PriorityQueue();
void insert(T value, float priority);
T extractMin();
T extractMax();
void remove(T value);
void decreaseKey(T value, float newPriority) {
remove(value);
insert(value, newPriority);
}
int getSize() {
return _size;
}
};
template <typename T>
PriorityQueue<T>::PriorityQueue() {
_head = nullptr;
_tail = nullptr;
_size = 0;
}
template <typename T>
void PriorityQueue<T>::insert(T value, float priority) {
if (_size == 0) { // List is empty
_head = new PriorityQueueEntry<T>(value, priority);
_tail = _head;
_size++;
return;
}
PriorityQueueEntry<T> *newEntry = new PriorityQueueEntry<T>(value, priority);
if (priority < _head->priority) {
newEntry->next = _head;
_head->prev = newEntry;
_head = newEntry;
} else if (_tail->priority <= priority) {
newEntry->prev = _tail;
_tail = newEntry;
newEntry->prev->next = newEntry;
} else {
PriorityQueueEntry<T> *compareEntry = _head;
while (compareEntry != nullptr && compareEntry->priority < priority) {
compareEntry = compareEntry->next;
}
if (compareEntry == nullptr)
throw MyExecption("PriorityQueue: Unable to insert an entry.");
newEntry->prev = compareEntry->prev;
newEntry->next = compareEntry;
compareEntry->prev = newEntry;
newEntry->prev->next = newEntry;
//TODO: Überlegen mit nur 1 Element...
}
_size += 1;
}
template <typename T>
T PriorityQueue<T>::extractMin() {
T result = nullptr;
if (_size == 0)
throw MyExecption("PriorityQueue is empty");
result = _head->value;
if (_size == 1) {
delete _head;
_head = nullptr;
_tail == nullptr;
} else {
_head = _head->next;
delete _head->prev;
_head->prev = nullptr;
}
_size -= 1;
return result;
}
template <typename T>
T PriorityQueue<T>::extractMax() {
T result;
if (_size == 0)
throw MyExecption("PriorityQueue is empty.");
result = _tail->value;
if (_size == 1) {
delete _tail;
_tail == nullptr;
_head == nullptr;
} else {
_tail = _tail->prev;
delete _tail->next;
_tail->next = nullptr;
}
_size--;
return result;
}
template <typename T>
void PriorityQueue<T>::remove(T value) {
if (_size == 0)
throw MyExecption("PriorityQueue is empty");
if (value == _head->value)
extractMin();
else if (value == _tail->value)
extractMax();
else {
PriorityQueueEntry<T> *compareEntry = _head->next;
while (compareEntry->next != nullptr && compareEntry->value != value) {
compareEntry = compareEntry->next;
}
if (compareEntry->next == nullptr)
throw MyExecption("PriorityQueue: Error Element couldn't be removed. Does it exist?");
compareEntry->prev->next = compareEntry->next;
compareEntry->next->prev = compareEntry->prev;
delete compareEntry;
_size -= 1;
}
}
#endif //OOA4_PRIORITYQUEUE_H