-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPQueue.h
More file actions
75 lines (70 loc) · 1.75 KB
/
PQueue.h
File metadata and controls
75 lines (70 loc) · 1.75 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
66
67
68
69
70
71
72
73
74
75
#ifndef P_QUEUE_H
#define P_QUEUE_H
#include "DblList.h"
#include "position.h"
template <typename T>
class PQueue: private DblList<T>{ //inherit from the DL List but don't make an is-a
//public methods
public:
PQueue(bool isMin); //min queue or not
virtual ~PQueue();
void add(T data);
T remove();
int size();
bool isEmpty();
T peek();
T removeTop();
private:
//private variable
bool isMinQ;
};
//checks if the queue is empty
template <typename T>
bool PQueue<T>::isEmpty(){
return DblList<T>::isEmpty();
}
//returns the size of the queue
template <typename T>
int PQueue<T>::size(){
return DblList<T>::size();
}
//sets the minimum
template <typename T>
PQueue<T>::PQueue(bool isMin){
isMinQ = isMin;
}
//default deconstructor
template <typename T>
PQueue<T>::~PQueue(){
}
//removes from the top
template <typename T>
T PQueue<T>::remove(){
if(isMinQ){
return DblList<T>::removeFront(); //smallest always at front
} else{
return DblList<T>::removeBack(); //largest always at back
}
}
//adds to the queue
template <typename T>
void PQueue<T>::add(T d){
if(isEmpty()){ //well, that's easy. Just add.
DblList<T>::addFront(d);
return;
}
ListNode<T>* newNode = new ListNode<T>(d);
int currIdx = 0;
ListNode<T>* currNode = DblList<T>::m_front;
//this while loop here is modded to compare the weights instead of just the data
while(currNode != NULL && currNode->m_data->getWeight() < d->getWeight()){ //from front, find the right
++currIdx;
currNode = currNode->m_next;
}
DblList<T>::add(currIdx,d); //this is a hack because it starts over looking from
}
template <typename T>
T PQueue<T>::removeTop() {
return DblList<T>::removeFront();
}
#endif