-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpriorityQueue.h
213 lines (164 loc) · 5.38 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//*****************************************************************************************************
//
// This header file defines a class template for an array-based priority queue list using a max
// heap structure.
//
//*****************************************************************************************************
#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
//*****************************************************************************************************
#include <iostream>
#include <new>
//*****************************************************************************************************
template <typename T>
class PriorityQueue {
private:
T *list;
int capacity;
int numValues;
bool _resize(int size);
void _heapify(int i);
public:
PriorityQueue(int size = 12);
~PriorityQueue();
bool enqueue(const T &value);
bool dequeue(T &value);
void display() const;
int getNumValues() const;
int getCapacity() const;
bool isEmpty() const;
bool isFull() const;
void clear();
};
//*****************************************************************************************************
template <typename T>
PriorityQueue<T>::PriorityQueue(int size) {
if (size < 12)
size = 12;
capacity = size;
numValues = 0;
list = new T[capacity];
}
//*****************************************************************************************************
template <typename T>
PriorityQueue<T>::~PriorityQueue() {
delete[] list;
capacity = 0;
numValues = 0;
}
//*****************************************************************************************************
template <typename T>
bool PriorityQueue<T>::_resize(int size) {
bool success = false;
int newCapacity;
T *newList = nullptr;
newCapacity = capacity + size;
newList = new (std::nothrow) T[newCapacity];
if (newList) {
for (int i = 0; i < numValues; ++i)
newList[i] = list[i];
delete[] list;
list = newList;
capacity = newCapacity;
success = true;
} else {
std::cerr << "\nError: memory allocation failed\n";
}
return success;
}
//*****************************************************************************************************
template <typename T>
void PriorityQueue<T>::_heapify(int i) {
bool stop = false;
int leftChild,
rightChild,
largest;
T temp = list[i];
leftChild = i * 2 + 1;
while ((leftChild < numValues) && (!stop)) {
rightChild = leftChild + 1;
if (rightChild >= numValues)
largest = leftChild;
else if (list[leftChild] > list[rightChild])
largest = leftChild;
else
largest = rightChild;
if (list[largest] > temp) {
list[i] = list[largest];
i = largest;
leftChild = i * 2 + 1;
} else {
stop = true;
}
}
list[i] = temp;
}
//*****************************************************************************************************
template <typename T>
bool PriorityQueue<T>::enqueue(const T &value) {
bool success = true;
int child,
parent;
child = numValues;
parent = (child - 1) / 2;
if (numValues == capacity)
success = _resize(10);
if (success) {
while ((child > 0) && (value > list[parent])) {
list[child] = list[parent];
child = parent;
parent = (parent - 1) / 2;
}
list[child] = value;
++numValues;
}
return success;
}
//*****************************************************************************************************
template <typename T>
bool PriorityQueue<T>::dequeue(T &value) {
bool success = false;
if (numValues > 0) {
value = list[0];
list[0] = list[numValues - 1];
--numValues;
success = true;
_heapify(0);
}
if (((capacity - numValues) >= 10) && ((capacity - 10) >= 12))
success = _resize(-10);
return success;
}
//*****************************************************************************************************
template <typename T>
void PriorityQueue<T>::display() const {
for (int i = 0; i < numValues; ++i)
std::cout << list[i] << std::endl;
}
//*****************************************************************************************************
template <typename T>
inline int PriorityQueue<T>::getNumValues() const {
return numValues;
}
//*****************************************************************************************************
template <typename T>
inline int PriorityQueue<T>::getCapacity() const {
return capacity;
}
//*****************************************************************************************************
template <typename T>
inline bool PriorityQueue<T>::isEmpty() const {
return (numValues == 0);
}
//*****************************************************************************************************
template <typename T>
inline bool PriorityQueue<T>::isFull() const {
return (numValues == capacity);
}
//*****************************************************************************************************
template <typename T>
inline void PriorityQueue<T>::clear() {
numValues = 0;
}
//*****************************************************************************************************
#endif