-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.cpp
126 lines (117 loc) · 3.31 KB
/
PriorityQueue.cpp
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
/*
* PriorityQueue.cpp
*
* Description: Priority Queue
* Class Invariant: The elements stored in this Priority Queue are always sorted.
*
* Author: Jiacheng Xu & Carla Louw
* Modified: March 17, 2019
*
*/
/* None of the current content of this file can be modified. */
#include "PriorityQueue.h"
// Default Constructor
PriorityQueue::PriorityQueue(){
head = NULL;
tail = NULL;
elementCount = 0;
}
// Destructor
PriorityQueue::~PriorityQueue(){
Node* ToBeRemoved = NULL;
Node* current = head;
while (current!= NULL) {
ToBeRemoved = current;
current = current->next;
delete ToBeRemoved;
ToBeRemoved = NULL;
}
elementCount = 0;
}
// Description: Returns the number of elements in the Priority Queue.
// Time Efficiency: O(1)
int PriorityQueue::getElementCount() const{
return elementCount;
}
// Description: Returns "true" is this Priority Queue is empty, otherwise "false".
// Time Efficiency: O(1)
bool PriorityQueue::isEmpty() const{
return elementCount == 0;
}
// Description: Inserts newElement in sort order.
// It returns "true" if successful, otherwise "false".
// Precondition: This Priority Queue is sorted.
// Postcondition: Once newElement is inserted, this Priority Queue remains sorted.
// Time Efficiency: O(n)
bool PriorityQueue::enqueue(HuffmansTree* newElement){
// if the queue is empty:
if (elementCount == 0){
Node* newNode = new Node(newElement);
head = newNode;
tail = newNode;
elementCount++;
return true;
}
// not empty:
Node* newNode = new Node(newElement);
Node* current = head;
while (current!= NULL && current->priority > newNode->priority) {
current = current->next;
}
if (current == NULL){
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
elementCount++;
return true;
}else {
Node* nextNode = current;
current = current->prev;
if (current == NULL){
head->prev = newNode;
newNode->next = head;
head = newNode;
elementCount++;
return true;
}
current->next = newNode;
nextNode->prev = newNode;
newNode->next = nextNode;
newNode->prev = current;
elementCount++;
return true;
}
}
// Description: Removes the element with the "highest" priority.
// It returns "true" if successful, otherwise "false".
// Precondition: This Priority Queue is not empty.
// Time Efficiency: O(1)
bool PriorityQueue::dequeue(){
if (elementCount == 0){
return false;
} else {
Node* ToBeRemoved = tail;
tail = tail->prev;
if (tail == NULL){
head = NULL;
} else {
tail->next = NULL;
}
delete ToBeRemoved;
ToBeRemoved = NULL;
elementCount--;
return true;
}
}
// Description: Returns the element with the "highest" priority.
// Precondition: This Priority Queue is not empty.
// Postcondition: This Priority Queue is unchanged.
// Exceptions: Throws EmptyDataCollectionException if this Priority Queue is empty.
// Time Efficiency: O(1)
HuffmansTree* PriorityQueue::peek() const{
if (tail){
return tail->data;
} else {
cout << "Cannot peek the queue since it is empty!" << endl;
}
}