Case Study Priority Queue
A hospital emergency department must manage patients based on the severity of their
condition rather than their order of arrival.
Each patient has:
o id
o name
o severity (higher value = more urgent).
Patients with higher severity are treated first.
If two patients have the same severity, the one who arrived earlier gets priority.
// Case Study: Hospital Patient Management using User-Defined Priority Queue (Array)
class Patient {
int id;
String name;
int severity;
public Patient(int id, String name, int severity) {
this.id = id;
this.name = name;
this.severity = severity;
@Override
public String toString() {
return "Patient[ID=" + id + ", Name=" + name + ", Severity=" + severity + "]";
// User-defined Priority Queue class using Array
class PriorityQueueArray {
private Patient[] queue;
private int size;
private int capacity;
public PriorityQueueArray(int capacity) {
this.capacity = capacity;
queue = new Patient[capacity];
size = 0;
// Check if empty
public boolean isEmpty() {
return size == 0;
// Check if full
public boolean isFull() {
return size == capacity;
// Insert patient (based on severity priority)
public void enqueue(Patient patient) {
if (isFull()) {
System.out.println("Queue is full! Cannot add " + patient);
return;
int i;
// Shift lower priority patients one step right
for (i = size - 1; i >= 0; i--) {
if (patient.severity > queue[i].severity) {
queue[i + 1] = queue[i]; // shift right
} else {
break;
queue[i + 1] = patient; // insert at correct position
size++;
System.out.println(patient + " added to queue.");
// Remove highest priority patient
public Patient dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty!");
return null;
Patient patient = queue[0]; // highest severity
// Shift all elements left
for (int i = 1; i < size; i++) {
queue[i - 1] = queue[i];
size--;
return patient;
}
// Peek highest priority
public Patient peek() {
if (isEmpty()) return null;
return queue[0];
// Display all patients
public void display() {
if (isEmpty()) {
System.out.println("Queue is empty!");
return;
System.out.println("Current waiting list:");
for (int i = 0; i < size; i++) {
System.out.println(queue[i]);
// Main Class
public class HospitalManagement {
public static void main(String[] args) {
PriorityQueueArray pq = new PriorityQueueArray(10);
// Add patients
pq.enqueue(new Patient(101, "Ali", 3));
pq.enqueue(new Patient(102, "Sara", 5));
pq.enqueue(new Patient(103, "Ahmed", 2));
pq.enqueue(new Patient(104, "Fatima", 4));
pq.enqueue(new Patient(105, "Omar", 1));
// Display patients
pq.display();
// Treat patients
System.out.println("\nTreating patients in order of severity:");
while (!pq.isEmpty()) {
System.out.println("Treating " + pq.dequeue());
OUTPUT
Patient[ID=101, Name=Ali, Severity=3] added to queue.
Patient[ID=102, Name=Sara, Severity=5] added to queue.
Patient[ID=103, Name=Ahmed, Severity=2] added to queue.
Patient[ID=104, Name=Fatima, Severity=4] added to queue.
Patient[ID=105, Name=Omar, Severity=1] added to queue.
Current waiting list:
Patient[ID=102, Name=Sara, Severity=5]
Patient[ID=104, Name=Fatima, Severity=4]
Patient[ID=101, Name=Ali, Severity=3]
Patient[ID=103, Name=Ahmed, Severity=2]
Patient[ID=105, Name=Omar, Severity=1]
Treating patients in order of severity:
Treating Patient[ID=102, Name=Sara, Severity=5]
Treating Patient[ID=104, Name=Fatima, Severity=4]
Treating Patient[ID=101, Name=Ali, Severity=3]
Treating Patient[ID=103, Name=Ahmed, Severity=2]
Treating Patient[ID=105, Name=Omar, Severity=1]
Case Study: CPU Process Scheduling using User-Defined Priority Queue (Java)
Problem Context
In an operating system, multiple processes compete for CPU time. Each process has:
PID (Process ID)
Execution Time (how long it needs the CPU)
Priority (higher number = more important/urgent process).
The CPU scheduler must decide which process runs first.
Using a priority queue, we ensure that higher-priority processes get CPU time earlier, just like
real OS schedulers.
Learning Outcomes
Students see how priority queues are directly used in CPU scheduling.
Understand user-defined implementation with arrays:
o Insertion: O(n) (shifting elements).
o Deletion (dequeue): O(1) (always take from front).
Real-world mapping:
o OS Process Scheduling
o Task Managers (Windows/Linux)
o Print Job Scheduling
o Network Packet Routing
// Case Study: CPU Process Scheduling using User-Defined Priority Queue (Array)
class Process {
int pid;
int burstTime;
int priority;
public Process(int pid, int burstTime, int priority) {
this.pid = pid;
this.burstTime = burstTime;
this.priority = priority;
@Override
public String toString() {
return "Process[PID=" + pid + ", BurstTime=" + burstTime + ", Priority=" + priority + "]";
// User-defined Priority Queue using array
class PriorityQueueArray {
private Process[] queue;
private int size;
private int capacity;
public PriorityQueueArray(int capacity) {
this.capacity = capacity;
queue = new Process[capacity];
size = 0;
public boolean isEmpty() {
return size == 0;
public boolean isFull() {
return size == capacity;
// Insert process into queue based on priority
public void enqueue(Process p) {
if (isFull()) {
System.out.println("Queue is full! Cannot add " + p);
return;
int i;
// Shift lower priority processes right
for (i = size - 1; i >= 0; i--) {
if (p.priority > queue[i].priority) {
queue[i + 1] = queue[i];
} else {
break;
queue[i + 1] = p;
size++;
System.out.println("Added: " + p);
// Remove highest priority process
public Process dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty!");
return null;
Process p = queue[0];
// Shift all left
for (int i = 1; i < size; i++) {
queue[i - 1] = queue[i];
size--;
return p;
public void display() {
if (isEmpty()) {
System.out.println("Queue is empty!");
return;
System.out.println("Current Process Queue:");
for (int i = 0; i < size; i++) {
System.out.println(queue[i]);
// Main Class
public class CPUScheduler {
public static void main(String[] args) {
PriorityQueueArray pq = new PriorityQueueArray(10);
// Add processes
pq.enqueue(new Process(1, 5, 3)); // Medium priority
pq.enqueue(new Process(2, 3, 5)); // Highest priority
pq.enqueue(new Process(3, 8, 1)); // Lowest priority
pq.enqueue(new Process(4, 6, 4)); // High priority
pq.enqueue(new Process(5, 2, 2)); // Low-medium priority
pq.display();
System.out.println("\nCPU Scheduling (executing processes by priority):");
while (!pq.isEmpty()) {
Process p = pq.dequeue();
System.out.println("Executing " + p + " ... Done.");
Output
Added: Process[PID=1, BurstTime=5, Priority=3]
Added: Process[PID=2, BurstTime=3, Priority=5]
Added: Process[PID=3, BurstTime=8, Priority=1]
Added: Process[PID=4, BurstTime=6, Priority=4]
Added: Process[PID=5, BurstTime=2, Priority=2]
Current Process Queue:
Process[PID=2, BurstTime=3, Priority=5]
Process[PID=4, BurstTime=6, Priority=4]
Process[PID=1, BurstTime=5, Priority=3]
Process[PID=5, BurstTime=2, Priority=2]
Process[PID=3, BurstTime=8, Priority=1]
CPU Scheduling (executing processes by priority):
Executing Process[PID=2, BurstTime=3, Priority=5] ... Done.
Executing Process[PID=4, BurstTime=6, Priority=4] ... Done.
Executing Process[PID=1, BurstTime=5, Priority=3] ... Done.
Executing Process[PID=5, BurstTime=2, Priority=2] ... Done.
Executing Process[PID=3, BurstTime=8, Priority=1] ... Done.