-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cpp
More file actions
66 lines (55 loc) · 1.65 KB
/
PriorityQueue.cpp
File metadata and controls
66 lines (55 loc) · 1.65 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
65
//
// Created by drago on 2017-06-24.
//
#include "PriorityQueue.h"
using namespace std;
template <class ElementType>
PriorityQueue<ElementType>::PriorityQueue(): elementCount(0), capacity(PriorityQueue::MAX_ELEMENTS){}
template <class ElementType>
int PriorityQueue<ElementType>::getElementCount() const {
return elementCount;
}
template <class ElementType>
bool PriorityQueue<ElementType>::isEmpty() const {
return getElementCount() == 0;
}
template <class ElementType>
bool PriorityQueue<ElementType>::enqueue(const ElementType& newElement){
if (getElementCount() < capacity) {
for (int i = 0; i < getElementCount(); i++) {
if (elements[i] > newElement){
// Insert before ith element
for (int j = getElementCount(); j > i; j--){
elements[j] = elements[j-1];
}
elements[i] = newElement;
elementCount++;
return true;
} else if (elements[i] == newElement) {
return false;
}
}
elements[elementCount] = newElement;
elementCount++;
return true;
}
return false;
}
template <class ElementType>
bool PriorityQueue<ElementType>::dequeue(){
if (isEmpty()) {
return false;
}
for (int i = 0; i < getElementCount(); i++){
elements[i-1] = elements[i];
}
elementCount--;
return true;
}
template <class ElementType>
ElementType PriorityQueue<ElementType>::peek() const throw(EmptyDataCollectionException){
if (isEmpty()){
throw EmptyDataCollectionException("Priority Queue is empty");
}
return elements[0];
}