Repository navigation
Expand file tree
/
Copy pathTreatmentPriorityQueue.java
More file actions
84 lines (76 loc) · 2.59 KB
/
Copy pathTreatmentPriorityQueue.java
File metadata and controls
84 lines (76 loc) · 2.59 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
/** A priority queue made with sorted linked nodes (not Java PriorityQueue). */
public class TreatmentPriorityQueue {
private TriageNode front;
private int size;
// INSERT: Lower numbers mean greater urgency (1 comes first).
// Patients with equal priority stay in arrival order.
public void enqueue(Patient patient) {
TriageNode newNode = new TriageNode(patient);
if (front == null || patient.getPriority() < front.patient.getPriority()) {
newNode.next = front;
front = newNode;
} else {
TriageNode current = front;
while (current.next != null
&& current.next.patient.getPriority() <= patient.getPriority()) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
size++;
}
// DELETE (normal priority queue rule): Treat the most urgent patient.
public Patient dequeue() {
if (front == null) return null;
Patient removed = front.patient;
front = front.next;
size--;
return removed;
}
// SEARCH: Find the patient with this ID.
public Patient search(int id) {
TriageNode current = front;
while (current != null) {
if (current.patient.getId() == id) return current.patient;
current = current.next;
}
return null;
}
// DELETE BY ID: Remove a patient who leaves before treatment.
public Patient deleteById(int id) {
TriageNode current = front;
TriageNode previous = null;
while (current != null && current.patient.getId() != id) {
previous = current;
current = current.next;
}
if (current == null) return null;
if (previous == null) front = current.next;
else previous.next = current.next;
size--;
return current.patient;
}
// DISPLAY: Patients are already sorted by medical priority.
public void display() {
if (front == null) {
System.out.println("Treatment priority queue is empty.");
return;
}
System.out.println("\nTREATMENT QUEUE (most urgent first)");
TriageNode current = front;
int position = 1;
while (current != null) {
System.out.println(position + ". " + current.patient);
current = current.next;
position++;
}
}
public Patient peek() {
if (front == null) return null;
return front.patient;
}
public int getSize() {
return size;
}
}