INSTITUTE OF ACCOUNTANCY ARUSHA
GROUP ASSIGNMENT
MODULE NAME : DATA STUCTURE AND ALGORITHMS
PROGRAMME : BCS III
SEMESTER : II
MODULE CODE : ITU 08209
FACILITATOR :ALMASI (MR KABEYA)
ACADEMIC YEAR : 2024/2025
MEMBERS:
NAME OF STUDENT REGISTRATION NUMBER
PATRICK OMARY KAOJOLE BCS-01-0051-2022
NEMAN N SILWIMBA BCS-01-0052-2022
NAMBA SONGORO MAULID BCS-01-0053-2022
JANETH M MASOLA BCS-01-0054-2022
MWANDU BUTOGWA MAHEGA BCS-01-0055-2022
COMFORT LODARU MOLLEL BCS-01-0056-2022
JOHN JOACHIM BCS-01-0057-2022
BWISO FRANCIS MWITA BCS-01-0058-2022
ELLEN J SEGERE BCS-01-0060-2022
DAUD SAMWEL MADEELKE BCS-01-0061-2022
LINDA JOHN MUNGURE BCS-01-0062-2022
SALAMA GODFREY SILASI BCS-01-0063-2022
VINCENT E LUNGWA BCS-01-0064-2022
KASIKA S KASIKA BCS-01-0065-2022
MAIMUNA R RASHID BCS-01-0066-2022
INTRODUCTION
Meaning of Queue Data Structure
A queue is a linear data structure that follows the FIFO (First-In-First-Out) principle. This
means the first element added to the queue will be the first one to be removed. Think of it like
a real-life queue at the hospital reception — the first patient who arrives is the first to be served.
How Enqueue and Dequeue Work
• Enqueue: This operation adds an element (e.g., a new patient) to the end (rear) of the queue.
• Dequeue: This operation removes an element from the front of the queue (the patient who
has been waiting the longest).
Why FIFO is Useful in Hospitals
1.Keeps Order and Fairness
FIFO ensures that patients are treated in the order they arrive. This helps prevent confusion and
frustration, especially in busy hospitals. Everyone knows their turn will come based on when
they arrived, which makes the process feel fair and organized.
2.Respects Arrival Time
In most outpatient departments, patients register and wait to see the doctor. FIFO ensures that
the first patient to register is also the first one to be called, unless there’s an emergency. This
respects people’s time and avoids skipping or favoritism.
3.Easy to Implement with Queues
A queue data structure naturally follows the FIFO rule. It has simple operations:
• enqueue to add a new patient to the end of the queue.
• dequeue to remove the patient at the front for treatment.
This makes FIFO logic easy to build into hospital management systems using programming.
Basic Operations of a Queue:
✓ Enqueue: Adds an item to the rear (back) of the queue.
✓ Dequeue: Removes an item from the front of the queue.
✓ Front: Gets the element at the front of the queue without removing it.
✓ Rear: Gets the element at the rear of the queue without removing it.
✓ isEmpty: Checks if the queue is empty.
✓ isFull: Checks if the queue is full (for fixed-size queues).
Time Complexity
Time complexity is a way to measure how long an algorithm takes to run as the size of the
input increases.
It tells us how the execution time grows when the number of items (called input size, usually
noted as n) increases.
Why is it Important?
Time complexity helps us to:
• Compare Algorithms
Time complexity lets us compare two algorithms.
For example, if one is O(n) and another is O(n²), the first one is faster for large input sizes.
• Predict Performance on Large Data
It helps us understand how an algorithm will perform when handling big data.
A solution that works fast for 10 items might become very slow with 10,000 if it has poor
time complexity.
• Choose Efficient Solutions
Knowing time complexity helps developers pick the most efficient and scalable approach
for solving problems.
Common Time Complexities (with examples):
Time Meaning Example
Complexity
0(1) Constant time – doesn’t grow with input Accessing an array
size element
0(n) Linear time – grows directly with input size Looping through an array
0(n²) Quadratic time – grows fast (n × n) Nested loops
0(log n) Logarithmic time – grows slowly Binary search
In Simple Words:
If your algorithm has:
• 0(1) time: it’s super-fast, same speed no matter how big the input is.
• 0(n) time: it gets slower as input grows.
• 0(n²) time: it gets much slower if input doubles.
Visualizing a Queue:
Imagine this queue of patients:
Front → [ Patient1, Patient2, Patient3 ] ← Rear
• Enqueue: Adding a patient (say, Patient4) to the rear
Front → [ Patient1, Patient2, Patient3, Patient4] ← Rear
• Dequeue: Removing the patient at the front (e.g., Patient1).
Front → [ Patient2, Patient3, Patient4] ← Rear
Why is a Queue a Natural Fit for Patient Handling?
1. Fairness: It ensures patients are served in the order they arrive, which is fair for a
normal outpatient department.
2. Real-time Handling: Queues can be extended with a priority mechanism (e.g., for
emergency patients) to insert them ahead of others.
3. Order Maintenance: It keeps a clear order of who arrived first and who should be
seen next.
4. Efficiency: Simple and fast to implement with low processing cost.
QUESTION TWO
Different types of Queues and their Application
1. Simple Queue (Linear Queue)
Definition:
A simple queue is a linear data structure that operates on the First-In-First-Out (FIFO)
principle. This means that the first element inserted into the queue is the first one to be
removed. Insertion happens at the rear end, and deletion happens at the front.
Application in Hospital System:
When to use: Normal patient flow where all patients are treated on a first-come, first-served
basis.
When patients come for non-urgent checkups or routine follow-ups, they can be served in the
order they arrive. So, normal patient flow where all patients are treated on a first-come, first-
served basis. This ensures fairness and simplicity.
Example
Used for general Outpatient Department (OPD).
Patients visiting for routine check-ups, blood pressure monitoring, or general medical advice
are queued and served in the order they register.
Why It's Appropriate:
• All patients are treated equally.
• No special urgency or critical condition involved.
• Simple and easy to manage.
2. Circular Queue
Definition:
A circular queue is a variation of the simple queue where the last position is connected back
to the first position to make a circle. This structure reuses empty spaces left by removed
elements, making it memory-efficient.
Application in Hospital System:
When to use: When resources are limited and reused, and efficient scheduling is needed.
In scenarios where resources (like hospital beds or diagnostic machines) are limited and
reused, circular queues can manage them efficiently.
Example
Used for equipment Scheduling and Bed Management
• Medical Equipment Scheduling:
Appointments for X-ray, MRI, or CT scans, where the same machine is used for
multiple patients throughout the day.
• Hospital Bed Management:
Assigning and reassigning a fixed number of beds to incoming patients after
discharges.
E.g., A hospital has 10 beds in a ward. As patients are discharged, new patients can occupy
those beds without wasting space in the queue.
Why It's Appropriate:
• Prevents resource wastage.
• Makes full use of available equipment or space.
• Avoids gaps in scheduling.
• Utilizes memory efficiently.
• Prevents the need for shifting elements as in a simple queue.
3. Priority Queue
Definition:
A priority queue is a type of queue in which each element is associated with a priority and is
served according to its priority level. The highest-priority elements are served before the
lower-priority ones.
Application in Hospital System:
When to use: In situations where some patients must be treated before others due to the
severity of their condition (Emergence patient prioritization)
In critical care situations, patients cannot be treated strictly based on their arrival time.
Instead, those with life-threatening conditions must be prioritized.so, critical cases must jump
the line
Example
• Emergency Room (ER) or Triage Unit:
A patient with a stroke or heart attack is given top priority over someone with a mild
fever.
• ICU Admissions:
Assigning limited ICU beds to the most critically ill patients.
Why It's Appropriate:
• Ensures urgent or life-threatening conditions are handled immediately.
• Helps save lives by not delaying care for critical patients.
• Reflects real-world emergency medical needs.
Therefore
Each type of queue serves a unique purpose in a hospital environment.
Simple queues are best for Normal patient flow Example OPD registration and pharmacy
line
Circular queues are best for efficient resources scheduling.
Priority queues are best for critical care and emergency cases (emergency patient
prioritization).
QUESTION THREE
Space Complexity and Memory Management Issues in Standard Queues
In a Smart Hospital Management System processing thousands of patients daily, the choice of
queue implementation significantly impacts memory efficiency and system performance.
Below, We evaluated the space complexity and potential memory management issues of a
standard queue, followed by efficient data structures for queue implementation.
Space Complexity of a Standard Queue
• Array-Based Queue:
o Space Complexity: O(n), where n is the maximum number of elements (patients) the
array can hold.
o Each patient object (containing fields like id, name, arrivalTime, priority,
doctorAssigned) occupies a fixed amount of memory. The array reserves space for n
such objects, regardless of how many are currently in the queue.
• Linked-List-Based Queue:
o Space Complexity: O(n), where n is the number of patients currently in the queue.
o Each node stores a patient object plus a pointer to the next node, adding overhead
compared to an array. However, memory is allocated dynamically, so only the actual
number of patients consumes space.
Potential Memory Management Issues
1.Array-Based Queue:
• Fixed Size Limitation: A fixed-size array may overflow if the number of patients
exceeds its capacity during peak hours (e.g., flu season). For example, an array sized
for 1000 patients can’t handle 1001 simultaneous registrations, causing system failures
or reject registrations.
• Wasted Memory: If the array is oversized to accommodate rare peak loads (e.g.,
10,000 slots for a typical 1000-patient day), most of the memory is unused, increasing
costs, especially in resource-constrained environments.
• Inefficient Space Reuse: As patients are dequeued, the front pointer moves forward,
leaving empty slots at the beginning. Without a mechanism like a circular queue, these
slots can’t be reused until the queue is reset or elements are shifted (costly, O(n)
operation).
2.Linked-List-Based Queue:
• Pointer Overhead: Each node requires extra memory for a pointer (e.g., 8 bytes on a
64-bit system), which can be significant for thousands of patients. For example, 10,000
patients might add 80,000 bytes of overhead.
• Memory Fragmentation: Frequent allocation (enqueue) and deallocation (dequeue) of
nodes can fragment memory, reducing efficiency and potentially causing allocation
failures in systems with limited memory.
• Dynamic Allocation Overhead: Allocating memory for each new patient is slower
than array indexing and may introduce latency in high-throughput scenarios, especially
if the system’s memory allocator is under stress.
3.Scalability Concerns:
• For a hospital processing thousands of patients daily, a poorly sized array or excessive
pointer overhead in a linked list can lead to performance bottlenecks or crashes.
• If the queue grows unpredictably (e.g., during emergencies), a fixed-size structure risks
overflow, while dynamic allocation risks fragmentation or allocation delays.
Efficient Data Structures for Queue Implementation
To address these issues, the following data structures can implement queues efficiently in the
hospital system:
1.Dynamic Arrays:
o Description: An array that resizes (e.g., doubles in size) when full, similar to ArrayList
in Java or vector in C++.
Benefits:
o Avoids overflow by growing as needed.
o Amortized O(1) enqueue time, as resizing is infrequent.
o Compact memory usage (no pointer overhead).
• Trade-Offs: Resizing operations are O(n) when they occur, and shrinking the array when
dequeuing many patients requires careful management to avoid thrashing.
• Hospital Fit: Ideal for unpredictable patient volumes, as it adapts to demand without
requiring a predefined maximum size.
2.Singly Linked Lists with Tail Pointer:
o Description: A linked list with a head pointer (for dequeue) and a tail pointer (for
enqueue) to ensure O(1) operations for both enqueue and dequeue.
Benefits:
o No fixed size, so no overflow risk.
o Memory usage scales with the actual number of patients.
o O(1) enqueue and dequeue with a tail pointer.
• Trade-Offs: Pointer overhead and potential fragmentation remain concerns, but these are
manageable with modern memory allocators.
• Hospital Fit: Suitable for scenarios with variable patient loads, especially when memory
fragmentation is mitigated by the system’s runtime environment.
3.Circular Buffers (Circular Queue):
o Description: An array-based queue where the rear pointer wraps around to the start
when it reaches the end, reusing slots freed by dequeues.
Benefits:
o O(1) enqueue and dequeue operations.
o Maximizes memory efficiency by reusing space, avoiding the wasted slots of a standard
array queue.
o Predictable memory usage (fixed array size).
• Trade-Offs: Still requires a predefined maximum size, risking overflow if undersized.
Dynamic resizing can be added but complicates implementation.
• Hospital Fit: Perfect for high-traffic outpatient departments with steady patient flows, as
it handles continuous enqueues and dequeues efficiently without shifting elements.
Recommendations for the Hospital System
Preferred Choice: A circular queue implemented with a dynamic array offers the best balance
for the hospital system. It provides:
o Efficient space usage via circular reuse.
o O(1) operations for enqueue and dequeue.
o Flexibility to resize if patient volumes exceed expectations (though resizing is rare with
proper capacity planning).
Mitigating Memory Issues:
• Estimate daily patient volume (e.g., 5000 patients) and set an initial array size slightly
above peak needs to minimize resizing.
• Use a memory pool for linked-list nodes if opting for a linked-list implementation to
reduce fragmentation.
• Monitor queue usage in real-time to trigger alerts if the queue nears capacity, allowing
staff to manage patient flow or scale resources.
• This approach ensures the Smart Hospital Management System handles thousands of
patients daily with minimal memory waste, robust scalability, and efficient
performance.
QUESTION FOUR
Problems with Array-Based Queues and Circular Queue Solution
Problems with Standard Array-Based Queues
When implementing a queue using a standard array for the Smart Hospital Management
System, where patients are continually added (enqueued) and removed (dequeued), several
issues arise:
• Wasted Space:
o In a standard array-based queue, elements are enqueued at the rear and dequeued
from the front. As patients are dequeued, the front pointer moves forward, leaving
empty slots at the beginning of the array.
o Once the rear pointer reaches the end of the array, the queue is considered "full,"
even if earlier slots (from previous dequeues) are empty. For example, if an array
of size 1000 has 500 patients dequeued, the first 500 slots are unused, but no new
patients can be added if the rear is at index 999.
• Inefficient Space Utilization:
In a high-traffic hospital processing thousands of patients daily, this inefficiency
leads to significant memory waste. A fixed-size array may either run out of space
quickly (causing overflow) or require an impractically large size, increasing
memory costs.
• Shifting Elements is Costly:
To reclaim space after dequeues, one could shift all elements left to move the
front back to index 0. However, this operation has a time complexity of O(n) per
dequeue, where n is the number of patients in the queue. For thousands of
patients, this is computationally expensive and slows down the system, causing
delays in patient processing.
• Queue Overflow:
If the array size is too small, the queue may become full during peak hours,
preventing new patients from being registered until others are dequeued. This can
disrupt hospital operations and frustrate patients.
Circular Queue Solution
A circular queue addresses these issues by treating the array as a circular buffer, where the
rear pointer wraps around to the beginning of the array when it reaches the end. Here’s how it
solves the problems:
• Eliminates Wasted Space:
o In a circular queue, when the rear pointer reaches the last index of the array, it wraps
around to index 0 (using modulo arithmetic: (rear + 1) % maxSize). This allows the
queue to reuse slots freed by dequeues.
o For example, if patient 1 is dequeued from index 0 and the rear reaches the end of the
array, the next patient can be enqueued at index 0, assuming it’s empty. This ensures
all array slots are utilized efficiently.
• Maintains O(1) Time Complexity:
Both enqueue and dequeue operations remain O(1) because there’s no need to shift elements.
The front and rear pointers are updated using simple arithmetic, making it ideal for real-time
patient processing in a busy hospital.
• Prevents Overflow (Within Capacity):
As long as the total number of patients in the queue doesn’t exceed the array’s capacity,
the circular queue can handle continuous enqueues and dequeues without running out of
space. This is particularly useful for managing steady patient flows throughout the day.
• Better Memory Management:
By reusing array slots, the circular queue maximizes memory efficiency, reducing the
need for an oversized array. For a hospital processing thousands of patients, this keeps
memory usage predictable and scalable.
Example in Hospital Context
• Standard Array Queue: Suppose an array of size 1000 is used. After 500 patients
are dequeued, the front is at index 500. If 500 more patients are enqueued, the rear
reaches index 999, and the queue is "full," even though indices 0–499 are empty. New
patients can’t be registered until more are dequeued or elements are shifted (costly).
• Circular Queue: After dequeuing 500 patients, the front is at index 500. When the
rear reaches index 999, the next enqueue wraps it to index 0. If indices 0–499 are
empty (due to prior dequeues), new patients can be added, ensuring continuous
operation without wasted space.
Implementation Note
To implement a circular queue, the system maintains:
• A fixed-size array to store patient objects.
• front and rear pointers to track the queue’s boundaries.
• A maxSize constant for the array capacity.
• Modulo operations to wrap pointers: rear = (rear + 1) % maxSize for enqueue, front =
(front + 1) % maxSize for dequeue.
This approach ensures the Smart Hospital Management System can handle high patient
volumes efficiently, avoiding the pitfalls of standard array-based queues while maintaining
fast and fair patient processing.
QUESTION FIVE
Algorithm for Basic Queue Operation (Enqueue) and Integration
Below is a detailed pseudocode for the enqueue operation in a circular queue, chosen for its
efficiency in handling continuous patient processing in a high-traffic hospital system. I’ll also
explain how it integrates with other modules of the Smart Hospital Management System.
Enqueue Algorithm (Circular Queue) pseudocode
Pseudocode
Function Enqueue(queue, patient, maxSize):
// Input: queue (circular queue with front, rear, data array), patient (object), maxSize (array
capacity)
// Check if queue is full
if (queue.rear + 1) % maxSize == queue.front:
Print "Queue is full. Cannot register patient."
Return False
// Update rear pointer circularly
queue.rear = (queue.rear + 1) % maxSize
// Add patient to rear
queue.data[queue.rear] = patient
// If queue was empty, set front to first element
if queue.front == -1:
queue.front = 0
Return True
// Example patient object: {id, name, arrivalTime, priority, doctorAssigned}
Explanation:
• The algorithm adds a patient to the rear of the circular queue. The rear pointer is
incremented using modulo (% maxSize) to wrap around to the start of the array if it
reaches the end, reusing empty slots.
• It checks if the queue is full by verifying if the next rear position equals the front. If
full, it rejects the enqueue operation.
• If the queue was empty (front == -1), the front pointer is set to 0 to mark the first
element.
• Time Complexity: O(1) for all operations (check, update, insert).
• The patient object contains fields like id (unique identifier), name, arrivalTime,
priority (e.g., 1 for emergency, 0 for routine), and doctorAssigned (based on
specialization or availability).
Integration with Hospital System Modules
The enqueue operation is a critical entry point for patient registration in the outpatient
department. It interacts with other system modules as follows:
• Doctor Alerting Module:
After a successful enqueue, the system checks the patient’s priority and the current queue
length. If the patient has high priority (e.g., emergency case) or the queue exceeds a threshold
(e.g., 10 patients for a doctor), it triggers a real-time notification to the doctorAssigned via
the hospital’s messaging system (e.g., SMS, app alert, or dashboard update).
Example: If a patient with priority = 1 is enqueued, the system immediately pings the
assigned doctor’s device to prepare for an urgent case.
• Medical Records Module:
Upon enqueue, the patient’s details (id, name, arrivalTime) are sent to the medical records
database to log their registration. This ensures accurate tracking for billing, audit trails,
and future reference.
The database update happens asynchronously to avoid slowing down the enqueue
operation, using a message queue or event-driven system to handle record insertion.
• Queue Monitoring Dashboard:
The system updates a real-time dashboard for hospital staff, reflecting the new patient in
the queue. This dashboard shows queue length, estimated wait times (based on average
doctor processing time), and highlights high-priority patients.
For example, after enqueue, the dashboard increments the queue length and recalculates
wait times for display.
• Patient Communication Module:
The system sends a confirmation to the patient (e.g., via SMS or app) with their queue
position and estimated wait time, improving transparency and patient experience.
Example Workflow
• A patient arrives and registers at the outpatient department. Their details are collected
(e.g., {id: 123, name: "John Doe", arrivalTime: "2025-04-28 10:00", priority: 0,
doctorAssigned: "Dr. Smith"}).
• The Enqueue function adds them to the circular queue.
• The system:
• Logs the patient in the medical records database.
• Updates the staff dashboard with the new queue state.
• Checks if Dr. Smith needs an alert (e.g., if the queue for Dr. Smith is long).
• Sends a confirmation SMS to John Doe: “You are #5 in the queue. Estimated
wait: 30 minutes.”
This integration ensures the enqueue operation not only manages the queue efficiently but
also triggers necessary actions across the hospital system, maintaining fairness, real-time
updates, and emergency prioritization.
REFERRENCES;
Cormen, T.H., Leiserson, C.E., Rivest, R.L. & Stein, C., 2022. Introduction to Algorithms.
4th ed. Cambridge, MA: MIT Press.
Sedgewick, R. & Wayne, K., 2011. Algorithms. 4th ed. Boston, MA: Addison-Wesley.
Goodrich, M.T., Tamassia, R. & Goldwasser, M.H., 2014. Data Structures and Algorithms in
Java. 6th ed. Hoboken, NJ: Wiley.
Knuth, D.E., 1997. The Art of Computer Programming, Volume 1: Fundamental Algorithms.
3rd ed. Reading, MA: Addison-Wesley.
Bhadauria, A. & Singh, A., 2019. ‘A review of queueing theory applications in healthcare’,
International Journal of Healthcare Management, 12(4), pp. 345–351.
doi:10.1080/20479700.2018.1473978.
Lakshmi, C. & Sivakumar, A.I., 2013. ‘Application of queueing theory in healthcare: A
literature review’, Operations Research for Health Care, 2(1-2), pp. 25–39.
doi:10.1016/j.orhc.2013.03.002.
Weiss, M.A., 2014. Data Structures and Algorithm Analysis in C++. 4th ed. Boston, MA:
Pearson.
GeeksforGeeks, 2023. Circular Queue | Set 1 (Introduction and Array Implementation).
Available at: https://www.geeksforgeeks.org/circular-queue-set-1-introduction-and-array-
implementation/ (Accessed: 28 April 2025).