blob: fe5333555327132214149ea1d6f9092b34172646 (
plain)
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
|
#ifndef PRIORITY_QUEUE_HPP
#define PRIORITY_QUEUE_HPP
#include <iostream>
template<typename T>
class PriorityQueue {
public:
// creates a new object of type T
PriorityQueue();
// deletes the array priority_array so that there are no memory leaks
~PriorityQueue();
// pushes element to the queue which is sorted by smallest element first
void push(const T& element);
// pops the first element from the queue and removes it while returning
// it
T pop();
private:
// size of the current queue
unsigned int size;
// capacity of the queue, this will just increase in multiples of 2
unsigned int capacity;
// array that will represent the priority queue
T *priority_array;
// resize the queue's array accordingly by some factor
void resize_queue(double factor);
// insert and element into the queue
// this assumes that the capacity is larger than the size
void insert_queue(const T& element, const unsigned int& loc);
// removes an element from the queue
T remove_queue(const unsigned int& loc);
};
template<typename T>
PriorityQueue<T>::PriorityQueue() : size(0), capacity(1) {
priority_array = new T;
}
template<typename T>
PriorityQueue<T>::~PriorityQueue() {
delete[] priority_array;
}
template<typename T>
void PriorityQueue<T>::push(const T& element) {
unsigned int insert_loc = 0;
if(size == capacity)
resize_queue(2);
while(insert_loc < size && element > priority_array[insert_loc])
++insert_loc;
insert_queue(element, insert_loc);
}
template<typename T>
T PriorityQueue<T>::pop() {
if(!size)
throw "Priority Queue is empty, no item to pop";
else if(size == capacity / 2)
resize_queue(0.5);
return remove_queue(0);
}
template<typename T>
void PriorityQueue<T>::resize_queue(double factor) {
capacity = (double)capacity * factor;
T *tmp_array = new T[capacity];
for(unsigned int i = 0; i < size; ++i)
tmp_array[i] = priority_array[i];
delete[] priority_array;
priority_array = tmp_array;
}
template<typename T>
void PriorityQueue<T>::insert_queue(const T& element, const unsigned int& loc) {
T *tmp_array = new T[capacity];
for(unsigned int i = 0; i < size + 1; ++i)
if(i == loc)
tmp_array[i] = element;
else if(i < loc)
tmp_array[i] = priority_array[i];
else
tmp_array[i] = priority_array[i - 1];
size++;
delete[] priority_array;
priority_array = tmp_array;
}
template<typename T>
T PriorityQueue<T>::remove_queue(const unsigned int& loc) {
T element;
T *tmp_array = new T[capacity];
for(unsigned int i = 0; i < size; ++i)
if(i == loc)
element = priority_array[i];
else if(i < loc)
tmp_array[i] = priority_array[i];
else
tmp_array[i - 1] = priority_array[i];
size--;
delete[] priority_array;
priority_array = tmp_array;
return element;
}
#endif
|