0% found this document useful (0 votes)
2 views

DSA Assignnment Final

The document outlines an assignment for a Bachelor of Engineering in Computer Science and Engineering, focusing on the design and implementation of a hospital management system using data structures. It details the use of queues for regular patients and priority queues for emergency cases, with patient details stored in linked lists. The program is implemented in C, emphasizing modular design and dynamic memory management to efficiently handle patient processing based on urgency.

Uploaded by

santhoshsd364
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DSA Assignnment Final

The document outlines an assignment for a Bachelor of Engineering in Computer Science and Engineering, focusing on the design and implementation of a hospital management system using data structures. It details the use of queues for regular patients and priority queues for emergency cases, with patient details stored in linked lists. The program is implemented in C, emphasizing modular design and dynamic memory management to efficiently handle patient processing based on urgency.

Uploaded by

santhoshsd364
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

BELAGAVI - 590018

Assignment -1
Submitted in partial fulfilment for the requirement of the award of degree
of
BACHELOR OF ENGINEERING
IN
Computer science and engineering
BY

Santhosh Devappa Dubari


[1EP23CS144]
Under the guidance of
Dr. Jagadevi Bakka

Jnana Prabha, East Point Campus, Virgonagar Post, Avalahalli,


Bangalore-560049
Department of Computer Science and Engineering 2024-2025
Page | 1
2024
Data structure and applications BCS304

Build a hospital system where patients are queued based on their arrival and
processed in FIFO order. Use queues for regular patients and priority queues
for emergency cases. Each patient’s details can be stored in a linked list.

INTRODUCTION:
In data structures, a queue is a linear data structure that operates in a First-In-First-
Out (FIFO) order, meaning the first element added to the queue is the first one to be
removed. This feature makes queues ideal for scenarios where tasks or entities need
to be processed in the order they arrive, such as in waiting lines, scheduling systems,
or, in this case, a hospital management system.
In a hospital environment, a queue can effectively manage the flow of patients by
organizing them based on their arrival time or the urgency of their needs. This
model allows for efficient and fair processing of patients, ensuring that those who
arrive first are served first, except in emergency cases, which are handled by a
separate priority queue for faster treatment.
Types of Queues
There are various types of queues, each with specific characteristics and
applications. Common types include:
1. Linear Queue: This is the most basic form of a queue where elements are
added at the rear (end) and removed from the front. Once an element is
removed, the memory is not reused, which can lead to inefficiency as the
queue fills up from the rear end.

2. Circular Queue: Unlike a simple queue, a circular queue connects the end of
the queue back to the front, creating a circular structure. This allows more
efficient memory use, as freed spaces at the front can be reused.

Page | 2 2024-25
Data structure and applications BCS304

3. Priority Queue: In a priority queue, each element has a priority associated


with it. Elements with higher priority are served before elements with lower
priority. If elements have the same priority, they are processed in the order
they were added (FIFO within the same priority level).

Linked list

A linked list is a linear data structure in which elements, called nodes, are stored in a
sequence but not in contiguous memory locations. Each node in a linked list
contains two main parts:
1. Data - The actual information or value.
2. Link - A reference to the next node in the sequence.
Types of Linked Lists
1. Singly Linked List:
In a singly linked list, each node contains a single link that points to the next
node in the sequence. The list starts with a head node (the first node) and ends
at the last node, which has a NULL link.

2. Doubly Linked List


Each node in a doubly linked list has two links: one pointing to the next node
and another pointing to the previous node. This bidirectional structure allows
traversal in both directions, making it more flexible but requiring more
memory for the additional link.

3. Circular Linked List


A circular linked list is a variation of a singly or doubly linked list where the
last node links back to the first node, creating a circular structure.

Page | 3 2024-25
Data structure and applications BCS304

4. Circular Doubly Linked List


In a circular doubly linked list, the last node’s next link points to the first node,
and the first node’s previous link points to the last node, creating a closed loop
with bidirectional traversal capabilities.

DESIGN:

Problem Analysis

The primary objective of the program is to design a patient management system for
a hospital that organizes patients into separate queues based on urgency: emergency
cases and regular cases. The system is required to:

1. Maintain a queue for regular patients (FIFO order).


2. Maintain a priority queue for emergency cases, with specific priority levels for
various emergency conditions.
3. Process patients in order of urgency, treating emergency cases first, followed by
regular patients.

Upon analyzing these requirements, we identified that:

- Regular patients follow a simple first-in, first-out (FIFO) order, which can be
managed with a standard queue.

- Emergency patients must be prioritized based on specific conditions (e.g., heart


attack, major accident). To achieve this, the emergency queue must follow a
priority-based order, with the most critical cases given precedence.

Based on this understanding, we designed the program around a linked list


implementation of queues. Linked lists are chosen because they allow for dynamic
memory allocation and efficient insertion and deletion, which are suitable for handling
an unknown number of patients without predefined limits.
Program Design and Sequence
Data Structure Design:

Page | 4 2024-25
Data structure and applications BCS304

We defined a Patient structure to store details for each patient, including an ID,
name, age, emergency status, and a pointer to the next patient.
Two separate queue structures (Queue struct pointers) manage patients: one for
regular patients and another for emergency cases.

Initialization:
Two queues, emergencyQueue and regularQueue, are initialized. The front and rear
pointers for each queue are set to NULL initially, indicating that the queues are
empty.

Patient Enqueueing:
Patients are added to the queues based on user input.
For regular patients, the system simply appends the new patient to the end of the
regular queue.
For emergency patients, the program prompts the user to select a specific emergency
condition (e.g., heart attack, major accident) and assigns a corresponding priority
level.
The emergency queue inserts each patient based on their emergency priority,
ensuring patients with higher urgency are positioned closer to the front of the queue.

Patient Processing (Treatment):


The treatment process operates in a loop, where it first checks if there are any
patients in the emergency queue.
If an emergency patient exists, they are removed from the front of the emergency
queue and treated.
If the emergency queue is empty, the next patient in the regular queue is treated.
This sequence ensures that emergency patients are always given priority over regular
patients, with treatment following a FIFO order within each queue.

Dynamic Memory Management:


As each patient is treated, their memory is freed to prevent memory leaks.
This approach allows the program to manage patients efficiently without wasting
memory, as patient nodes are allocated and freed dynamically.

This design allows for efficient patient management, prioritizing emergency cases
while ensuring a structured treatment process that respects both the urgency and
order of arrival for each patient.

Page | 5 2024-25
Data structure and applications BCS304

IMPLIMENTATION :

The hospital queuing system program is implemented using C, and it follows a


modular design. Each core functionality is encapsulated in separate functions,
providing a clear and efficient way to manage regular and emergency patient queues.
Below are the key components and their descriptions.

The program uses two main data structures:


• Patient Structure: This structure holds the patient's details, including ID,
name, age, emergency type, and a pointer to the next patient. This helps
manage patient nodes in the queue.
• Queue Structure: This structure manages a queue for patients, with front and
rear pointers to keep track of the start and end of the queue.

Creating a New Patient


The createPatient function allocates memory for a new patient and assigns their
details, including ID, name, age, and emergency type. This function is used to
create a patient node before adding them to the appropriate queue.

Page | 6 2024-25
Data structure and applications BCS304

Adding a Regular Patient (FIFO)


The enqueueRegular function appends a patient to the rear of the regular queue,
following the FIFO principle. If the queue is empty, the new patient becomes both
the front and rear of the queue.

Adding an Emergency Patient (Priority-Based)


The enqueueEmergency function inserts patients based on the emergency type
priority. Patients with a more urgent emergency type are positioned closer to the
front of the queue.

Page | 7 2024-25
Data structure and applications BCS304

Treating Patients Based on Priority


The treatNextPatient function processes the next patient, giving priority to those in
the emergency queue. If there are no emergency patients, it treats the next patient
from the regular queue. The treated patient is removed from the queue and their
memory is freed.

Page | 8 2024-25
Data structure and applications BCS304

COMPLETE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define Patient structure


typedef struct Patient {
int id;
char name[50];
int age;
int emergencyType; // 1 for heart attack, 2 for pregnant woman, etc.
struct Patient *next;
} *PATIENT;

// Define Queue structure as a pointer type


typedef struct Queue {
PATIENT front;
PATIENT rear;
} *QUEUE;

// Function to initialize a queue


void initQueue(QUEUE queue) {
queue->front = NULL;
queue->rear = NULL;
}

// Function to create a new patient


PATIENT createPatient(int id, const char *name, int age, int emergencyType) {
PATIENT newPatient = (PATIENT)malloc(sizeof(struct Patient));
newPatient->id = id;
strcpy(newPatient->name, name);
newPatient->age = age;
newPatient->emergencyType = emergencyType;
newPatient->next = NULL;
return newPatient;
}

// Function to enqueue a patient in the regular queue (FIFO)


void enqueueRegular(QUEUE queue, PATIENT patient) {
if (queue->rear == NULL) {
// Queue is empty, so front and rear both point to new patient
queue->front = patient;
queue->rear = patient;
Page | 9 2024-25
Data structure and applications BCS304

} else {
// Queue is not empty, add patient to the rear
queue->rear->next = patient;
queue->rear = patient;
}
}

// Function to enqueue a patient in the emergency queue (based on emergency type)


void enqueueEmergency(QUEUE queue, PATIENT patient) {
if (queue->front == NULL || patient->emergencyType < queue->front->emergencyType)
{
// Insert at the front if it's the most urgent
patient->next = queue->front;
queue->front = patient;

if (queue->rear == NULL) {
queue->rear = patient;
}
} else {
// Traverse the list until we find the correct position
PATIENT current = queue->front;
while (current->next != NULL) {
// Break if we find a patient with a lower priority
if (current->next->emergencyType > patient->emergencyType) {
break;
}
current = current->next;
}

// Insert the new patient after `current`


patient->next = current->next;
current->next = patient;

// Update the rear if patient is added at the end


if (patient->next == NULL) {
queue->rear = patient;
}
}
}

// Function to treat the next patient (emergency patients first)


void treatNextPatient(QUEUE emergencyQueue, QUEUE regularQueue) {
PATIENT patient;

Page | 10 2024-25
Data structure and applications BCS304

// Check if there is an emergency patient to treat


if (emergencyQueue->front != NULL) {
// Remove the front patient from the emergency queue
patient = emergencyQueue->front;
emergencyQueue->front = patient->next;

// If the emergency queue is now empty, set rear to NULL


if (emergencyQueue->front == NULL) {
emergencyQueue->rear = NULL;
}

// Print treated emergency patient details


printf("Treating emergency patient: %s (ID: %d, Age: %d, Emergency Type:
%d)\n",
patient->name, patient->id, patient->age, patient->emergencyType);
}
else if (regularQueue->front != NULL) {
// No emergency patients, so treat the next regular patient
patient = regularQueue->front;
regularQueue->front = patient->next;

// If the regular queue is now empty, set rear to NULL


if (regularQueue->front == NULL) {
regularQueue->rear = NULL;
}

// Print treated regular patient details


printf("Treating regular patient: %s (ID: %d, Age: %d)\n",
patient->name, patient->id, patient->age);
}
else {
// No patients to treat in either queue
printf("No patients to treat.\n");
return;
}

// Free memory for the treated patient


free(patient);
}

// Function to input patient details and add to the appropriate queue


void inputPatientDetails(QUEUE emergencyQueue, QUEUE regularQueue) {
int id, age, emergencyStatus, emergencyType;
char name[50];
Page | 11 2024-25
Data structure and applications BCS304

printf("Enter Patient ID: ");


scanf("%d", &id);
printf("Enter Patient Name: ");
scanf("%s", name);
printf("Enter Patient Age: ");
scanf("%d", &age);
printf("Enter 1 for Emergency or 0 for Regular: ");
scanf("%d", &emergencyStatus);

if (emergencyStatus) {
// Ask for specific emergency type
printf("Select Emergency Type:\n");
printf("1 - Heart Attack\n2 - Pregnant Woman\n3 - Major Accident (Heavy
Bleeding)\n4 - Accident with Minor Danger\n");
printf("Enter choice (1-4): ");
scanf("%d", &emergencyType);

PATIENT newPatient = createPatient(id, name, age, emergencyType);


enqueueEmergency(emergencyQueue, newPatient);
} else {
// Default emergencyType for regular patients is 5
PATIENT newPatient = createPatient(id, name, age, 5);
enqueueRegular(regularQueue, newPatient);
}
}

int main() {
// Create and initialize queues
QUEUE emergencyQueue = (QUEUE)malloc(sizeof(struct Queue));
QUEUE regularQueue = (QUEUE)malloc(sizeof(struct Queue));
initQueue(emergencyQueue);
initQueue(regularQueue);

int numPatients, i;
printf("Enter the number of patients to add: ");
scanf("%d", &numPatients);

// Input details for each patient


for (i = 0; i < numPatients; i++) {
printf("\nEnter details for patient %d:\n", i + 1);
inputPatientDetails(emergencyQueue, regularQueue);
}

Page | 12 2024-25
Data structure and applications BCS304

// Process patients in order of priority


printf("\nProcessing patients:\n");
while (emergencyQueue->front != NULL || regularQueue->front != NULL) {
treatNextPatient(emergencyQueue, regularQueue);
}

// Free allocated memory for queues


free(emergencyQueue);
free(regularQueue);

return 0;
}
OUTPUT:

Page | 13 2024-25
Data structure and applications BCS304

Page | 14 2024-25
Data structure and applications BCS304

APPLICATIONS:

1. Airport Security and Check-In Management:


Airports use queuing systems to manage the flow of passengers through
security checks, immigration, and boarding. Priority is often given to travelers
with tight connections, premium ticket holders, and those requiring assistance.
By categorizing passengers, these systems improve efficiency and reduce wait
times, allowing timely boarding and departure.

2. Customer Service in Banks and Financial Institutions:


Banks often have separate queues for different types of transactions, with
priority given to customers with urgent needs, such as large withdrawals or
business clients. Systems like this can prioritize customer service based on the
type of service required, ensuring high-value or urgent cases are handled
swiftly while keeping general queues organized.

3. Online Order Fulfillment in E-commerce Warehouses:


In e-commerce, fulfillment centers handle orders based on urgency, customer
type, or special handling requirements. For example, priority is often given to
expedited or same-day shipping orders. These priority-based systems ensure
that high-priority packages are processed faster to meet delivery deadlines,
optimizing the workflow and improving customer satisfaction.

4. Customer Support and Technical Assistance Centers:


Call centers and help desks use similar queuing models to triage customer
inquiries. Issues flagged as urgent, such as service outages or technical
failures, receive priority over general inquiries. This ensures that critical
issues are addressed first, improving service quality and reducing downtime
for customers experiencing major issues.

5. Ambulance Dispatch and Emergency Response Systems


Emergency services use priority dispatch systems to allocate resources such
as ambulances or firefighters based on the severity of the incident. Calls
related to life-threatening situations, like cardiac arrests or major accidents,
receive top priority. This structured response system helps ensure rapid
intervention where it’s most needed.
Page | 15 2024-25
Data structure and applications BCS304

REFERANCES:
References related to queuing and priority systems:
Websites
1. Khan Academy - Probability and Statistics
Khan Academy
This site offers lessons on probability and statistics, which are foundational
for understanding queuing theory.
2. Coursera - Operations Management: Analysis and Improvement
Methods
Coursera
This course includes sections on queuing systems in operations management.

References related to Linked list:


Websites
1.GeeksforGeeks - Linked List
https://www.geeksforgeeks.org/data-structures/linked-list/
1. TutorialsPoint - Linked List Data Structure
https://www.tutorialspoint.com/data_structures_algorithms/linked_list_data_s
tructure.htm

Page | 16 2024-25

You might also like