-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPatientQueue.h
More file actions
276 lines (229 loc) · 6.23 KB
/
Copy pathPatientQueue.h
File metadata and controls
276 lines (229 loc) · 6.23 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// بسم الله الرحمن الرحيم
// "وَأَنْ لَيْسَ لِلْإِنسَانِ إِلَّا مَا سَعَىٰ"
// Free Palestine
#ifndef PATIENTQUEUE_H
#define PATIENTQUEUE_H
#include <bits/stdc++.h>
#include "Patient.h"
using namespace std;
#define nl '\n'
// QueueNode class
class QueueNode
{
public:
Patient patient;
QueueNode *next;
// Constructors
QueueNode() {}
QueueNode(Patient &p) : patient(p), next(nullptr) {}
};
// PatientQueue class
class PatientQueue
{
private:
QueueNode *head;
QueueNode *tail;
int patientCount;
public:
// Default constructor
PatientQueue() : head(nullptr), tail(nullptr), patientCount(0) {};
~PatientQueue() { clear(); }
// Cheack if the queue is empty or not.
bool isEmpty()
{
return (head == nullptr);
}
int getQueueCount()
{
return patientCount;
}
QueueNode *getHead()
{
return head;
}
void MoveHead()
{
head = head->next ;
}
void SetTailToNull()
{
tail = nullptr ;
}
// Enqueue to add patient(-MoRa-)
void enqueue(Patient patient)
{
QueueNode *newPatient = new QueueNode(patient);
if (isEmpty())
{
head = tail = newPatient;
}
else
{
tail->next = newPatient;
tail = newPatient;
}
patientCount++;
}
// Dequeue to delete patient(-MoRa-)
Patient dequeue()
{
if (isEmpty())
{
cout << "There are no patients available." << nl;
return Patient();
}
patientCount--;
if (head == tail)
{
Patient tempVal = head->patient;
delete head;
head = tail = nullptr;
return tempVal;
}
Patient tempVal = head->patient;
QueueNode *temp = head;
head = head->next;
delete temp;
return tempVal;
}
// peek to get current patinet which on the top
Patient next()
{
if (isEmpty())
{
cout << "There are no patients available." << nl;
return Patient();
}
return head->patient;
}
Patient searchPatient(const Patient &other)
{
if (isEmpty())
{
cout << "There are no patients available." << nl;
return Patient();
}
int id = other.getId();
QueueNode *trav = head;
while (trav != nullptr)
{
if (trav->patient.getId() == id)
return trav->patient;
trav = trav->next;
}
cout << "This patient is not in this queue!" << nl;
return Patient();
}
Patient searchPatient(int id) // overloaded function to search directly by id
{
if (isEmpty())
{
cout << "There are no patients available." << nl;
return Patient();
}
QueueNode *trav = head;
while (trav != nullptr)
{
if (trav->patient.getId() == id)
return trav->patient;
trav = trav->next;
}
cout << "This patient is not in this queue!" << nl;
return Patient();
}
bool searchPatientbool(int id) // overloaded function to search directly by id
{
if (isEmpty())
{
return 0 ;
}
QueueNode *trav = head;
while (trav != nullptr)
{
if (trav->patient.getId() == id)
return 1 ;
trav = trav->next;
}
return 0 ;
}
// Add this inside class PatientQueue in PatientQueue.h
Patient removeById(int id)
{
if (isEmpty())
{
cout << "Queue is empty. Cannot remove patient." << nl;
return Patient(); // Return empty patient
}
// SCENARIO 1: The patient to remove is at the HEAD
if (head->patient.getId() == id)
{
QueueNode *temp = head;
Patient removedData = head->patient;
head = head->next; // Move head forward
// If the queue becomes empty, update tail to null
if (head == nullptr)
{
tail = nullptr;
}
delete temp;
patientCount--;
return removedData;
}
// SCENARIO 2: The patient is somewhere else (Middle or Tail)
QueueNode *prev = head;
QueueNode *curr = head->next;
while (curr != nullptr)
{
if (curr->patient.getId() == id)
{
Patient removedData = curr->patient;
// Link previous node to the next node (skipping current)
prev->next = curr->next;
// If we are removing the TAIL, update tail to point to prev
if (curr == tail)
{
tail = prev;
}
delete curr;
patientCount--;
return removedData;
}
// Move pointers forward
prev = curr;
curr = curr->next;
}
// SCENARIO 3: Patient ID was not found
cout << "Patient with ID " << id << " not found in the queue." << nl;
return Patient();
}
// display out patinets queue
void display()
{
if (isEmpty())
return void(cout << "there are no patinets in the queue." << nl);
cout << "==================================" << nl;
cout << " PATIENT QUEUE SUMMARY " << nl;
cout << "==================================" << nl;
cout << "Total Patients: " << patientCount << nl;
cout << "----------------------------------" << nl;
QueueNode *trav = head;
int index = 1;
while (trav != nullptr)
{
cout << "Patient #" << index << nl;
trav->patient.display();
trav = trav->next;
index++;
}
cout << "========== END OF QUEUE ==========" << nl;
}
// save memory leak
void clear()
{
while (!isEmpty())
dequeue();
}
PatientQueue(const PatientQueue &) = delete;
PatientQueue &operator=(const PatientQueue &) = delete;
};
#endif