-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppointmentQueue.java
More file actions
executable file
·109 lines (86 loc) · 2.64 KB
/
Copy pathAppointmentQueue.java
File metadata and controls
executable file
·109 lines (86 loc) · 2.64 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hospitalmanagementsystem;
public class AppointmentQueue {
// INNER CLASS: QueueNode
class QueueNode {
Appointment data;
QueueNode next;
public QueueNode(Appointment data) {
this.data = data;
this.next = null;
}
}
private QueueNode front;
private QueueNode rear;
public AppointmentQueue() {
this.front = null;
this.rear = null;
}
public boolean isEmpty() {
return front == null;
}
// Adds a new appointment to the end of the queue.
// Critical for maintaining fair, First-Come-First-Served order for doctor visits.
// Time Complexity: O(1) (Direct access via 'rear' pointer)
public void enqueue(Appointment appointment) {
QueueNode newNode = new QueueNode(appointment);
if (rear != null) {
rear.next = newNode;
}
rear = newNode;
if (front == null) {
front = newNode;
}
}
// Removes and returns the appointment at the front of the queue.
// Used when the doctor is ready to examine the next patient.
// Time Complexity: O(1) (Direct access via 'front' pointer)
public Appointment dequeue() {
if (isEmpty()) {
return null;
}
Appointment data = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return data;
}
// Traverses the queue to display all waiting patients in order.
// Useful for showing the doctor how many patients are waiting.
public void display() {
if (isEmpty()) {
System.out.println(" Queue is empty.");
return;
}
QueueNode current = front;
int position = 1;
while (current != null) {
System.out.println(" " + position + ". " + current.data);
current = current.next;
position++;
}
}
public int size() {
if (isEmpty()) return 0;
int count = 0;
QueueNode current = front;
while (current != null) {
count++;
current = current.next;
}
return count;
}
public QueueNode getFront() {
return front;
}
public void setFront(QueueNode front) {
this.front = front;
}
public void setRear(QueueNode rear) {
this.rear = rear;
}
}