-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityQueueLL.cpp
More file actions
119 lines (102 loc) · 1.96 KB
/
priorityQueueLL.cpp
File metadata and controls
119 lines (102 loc) · 1.96 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
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
using namespace std;
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include "priorityQueueLL.h"
priorityQueueLL::priorityQueueLL(){
}
priorityQueueLL::~priorityQueueLL(){
}
//constructor and deconstructor, not used
int priorityQueueLL::comparePatient(patientLL * p_a, patientLL * p_b)
//Helper function to compare priorities, takes treatment into account
//returns 1 if p_a < p_b
//returns 0 if p_a = p_b
//returns -1 if p_a > p_b
{
if (p_a->priority < p_b->priority)
{
return 1;
}
//if p_a and p_b are equal, check treatment time
else if (p_a->priority == p_b->priority)
{
if(p_a->treatment < p_b->treatment)
{
return 1;
}
else if (p_a->treatment == p_b->treatment)
{
return 0;
}
else
{
return -1;
}
}
else if(p_a->priority > p_b->priority)
{
return -1;
}
}
void priorityQueueLL::push(string n, int p, int t) {
patientLL* newPatient = new patientLL;
newPatient->name = n;
newPatient->priority = p;
newPatient->treatment = t;
if(!head)
//heads null, aka list is empty
{
head = newPatient;
return;
}
if (0 > comparePatient(head, newPatient))
{
newPatient->next = head;
head = newPatient;
return;
}
patientLL *temp = head;
//temp for looping starting at head
while(temp->next)
//keep looping till the enddddd
{
if(0 > comparePatient(temp->next, newPatient))
//compare the paient we are adding with the next temp one so we can add it before
{
newPatient->next = temp->next;
temp->next = newPatient;
return;
//we found it, add it and return
}
temp = temp->next;
//move to the next
}
temp->next = newPatient;
return;
}
void priorityQueueLL::pop()
{
if(!head->next)
//head is the only one
{
delete head;
head = NULL;
}
else
//head is not the only one
{
patientLL *temp = head;
head = head->next;
delete temp;
//move head and clear the memory
}
}
patientLL* priorityQueueLL::top()
{
return head;
}