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

Programming Report

The report details a mini-project aimed at developing a patient diagnosis system using linked lists in C programming. It outlines the structure and functionalities needed for managing patient records, diagnostics, and doctor information, including adding, deleting, editing, and searching records. The document also discusses the implementation of a user interface and algorithms for linked list operations, emphasizing dynamic data management and user interaction.

Uploaded by

misscharchar247
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Report

The report details a mini-project aimed at developing a patient diagnosis system using linked lists in C programming. It outlines the structure and functionalities needed for managing patient records, diagnostics, and doctor information, including adding, deleting, editing, and searching records. The document also discusses the implementation of a user interface and algorithms for linked list operations, emphasizing dynamic data management and user interaction.

Uploaded by

misscharchar247
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

University of Ghardaia

Department of Mathematics and Computer Science


18 May 2024

ASD2
Programming Report of Mini-Project 2

Introduction:
In this mini-project that has been presented to us, we are concerned with making a patient diagnosis system
employing linked lists to store and manage a healthcare environment efficiently, this will take form as a
computer program written in the C programming language.

To realize this tangible result, we have gone with a paradigm similar in merit to the standard scientific
method; that is:
Observation → Question → Hypothesis → Experiment → Analysis → Conclusion

However, it was slightly altered to account for the nature of mathematical and logical problems into:
Observation → Question → Analysis → Hypothesis → Experiment → Conclusion

We also had to take into consideration some obstacles that may arise such as:
-The inherent limitations of the computational machine that we are using.
-The syntactic restrictions imposed by the C programming language.

Problematic:
The preceding problem can be broken down into 4 individual parts

Part 1: Patients
In this part, we want to define a patient record with various fields, namely:

● Patient ID
● Full Name
● Age
● Gender
● Height

We also want to define some actions involving the management of said records, such as:

● Adding a new patient


● Deleting a patient
● Editing a patient
● Printing a patient
● Searching a patient
Part 2: Diagnostics
We are then asked to extend the program some more to Implement some elements to account for
diagnostics, much similar to patients, we have to declare some fields, namely:

● Patient ID
● Diagnosis
● Diagnosis date
● Next appointment date

And we also have to implement the same actions as the patient’s for the diagnostics:

● Adding a new diagnostic


● Deleting a diagnostic
● Editing a diagnostic
● Printing a diagnostic
● Searching a diagnostic

Part 3: Doctor
We have to then make another system for recording doctors, this will be done the same way as the
previously aforementioned parts, the doctor will have 3 fields:

● Doctor ID
● Full name
● Specialty

The doctor system will have the same functions as the previous 2 systems.

Part 4: Link Up
Here we have to link up the diagnoses to their respective doctor who performs them which will be identified
by hi doctor ID.

Analysis:
To analyze this problem we must break down each sub-problem and articulate each input and output that
they will take and give, since some aspects of this solution are rather trivial we will only focus on the main
problems, those being:

1) This problem requires the use of dynamic data structures since the amount of space needed for this
program is initially unknown, thus we will implement a clever tool called a linked list in order to
dynamically hold the information necessary for this program .

2) The nature of this problem demands a sort of graphical user interface and hence we will have to
improvise one in the command prompt along with a system for navigating different interfaces based
on user input.

3) This project will require a lot of repetitive code and so it would make sense to utilize an abstraction
method called modules or subprogramming, which will then let us reuse our code but in a more
readable and maintainable manner.
We will discuss the problems stated above in much more details in the algorithm and programming part, for
now we will concentrate on the fundamentals.

Dynamic Data Management.

We have already determined that the dynamic data management will be implemented using linked lists.
Linked lists are a data structure consisting of a sequence of elements, each containing a pointer to the next
element in the sequence containting its address as shown in the diagram below. This allows for efficient
insertion and deletion of elements, as the structure does not require continuous memory allocation.

We have to then understand how each of the operations on the linked list will take place, and for that we will
have to do some analysis

Adding an Element

To add an element we have to do an interesting manoeuvre called prepending since it is easier and since the
order of our items does not matter, its steps are as follows

1. Create an empty record node for either one of our examples (patient, diagnostic, or doctor).
2. Fill in the information we need.
3. Set it to point at what the head is pointing at.
4. Set the head to point at our new node.

We have gone with inputing the data straight from the user rather than pluging them as input into a function.
Deleting an Element
The process for this is fairly basic and just requires some more techniques, they go as follows.

1. Find our target node and set a pointer to point to it.


2. Set the element before it to point to the element after it.
3. Free the node selected back to memory.

Updating an Element
This is by far the simplest action as we just need to locate our target node by inputing its address and
refilling all our information.

Printing an Element
Here we will find our target node and then write each element in a neatly formatted string.

Searching an Element
To implement this we will take an ID as input and give us the address of the node containing said ID by:

1. Declaring a pointer that points to the head


2. Comparing the input ID with the ID of the current node we are pointing to
3. Moving the pointer to the next address of the current
4. Repeat steps 2 and 3 until we find the node or not in which case we output NULL
User Interface.

We have to present the user with a user interface that they can interact with and navigate around, the user
should be able to switch between interfaces or go back to the previous interface or even exit the program
should he desire to.

The way we have implemented this is by using nested do while loops and, a global state variable, and switch
statements such that the program will initially start with a global state of 1 and display some options, it will
keep prompting us for a choice until we either choose to exit the program or pick an option, once we do, it will

increase the state and put us in another do while loop on the condition that the state does not change,
because if it does, that means we have either went back to state 1 ( the starting interface ) or are going to
perform an option after which we will repeat prompting the user for their next action.

Algorithm:
To get the expected result we must first evaluate our inputs and outputs and create some base algorithms
that we will use as the primary building blocks for our full-fledged program, we will state the algorithms
necessary for each part of this problem along with their respective application:

Part 1: Linked List functions


Our primary focus in this part is to create functions to perform operations on our linked list, once we define
the general structure we can apply this to any of the types of our elements, . Hence we have:

We can assume that the node record will look something like this:

Type: node = record


Field1 = datatype1
Field2 = datatype2
Field3 = datatype3
….
Next = ^node /* pointer to node */
End

We can then define a function to add a node to the list.


Function: add element
Input:none
Output: none
Procedure: newNode: ^node
Start
newNode = listHead /* we can do this since our listHead will be a global variable */
write(“Enter parameter 1”)
Read (paramter1)

next(newNode) = listHead
listeHead = newNode
End
Then define a function to delete a node from the list.
Procedure: delete element
Input: target /* target node address */
Output: none
Variables: curr, next: ^node
Start
curr = listHead
if( listHead == target )
Begin
listHead = next(listHead)
free(curr)

Else
while(curr != NULL)
Begin
If (next(curr) == target)
Begin
Next = next(next(curr))
free(next(curr))
curr = next
End
End
End
End

And then another to edit the nodes


Procedure: Update node
Input: target /* target node address */
Output: none
Variables: choice:integer
Start
writeln(“what would you like to change”)
writeln(“1) value 1”)
writeln(“2) value 2”)

writeln(“N) value N”)
read(choice)
Switch(choice)
Case 1:
read(val1(target))
Case 2:
read(val2(target))
.
.
.
Case N:
read(valN(target))
End
And then another to print the nodes
Procedure: print node
Input: target /* target node address */
Output: none
Variables:
Start
write(“value 1:” , value1(target), “value 2:” , value2(target) . . . “value N” , valueN(target))
End

And finally we can add a function to search patients using their IDs
Function: search
Input: ID : integer
Output: target: ^node
Variables: iterator: ^node
Start
Iterator = listHead
while( iterator != NULL)
Begin
if(iterator^.ID == ID)
Return iterator
Else
Iterator = next(iterator)
End

Part 2: User interface


In order to obtain the result we want, we are gonna run two do while loops inside each other as follows.

Algorithm: User interface


Input: none.
Output: none.
Global variables: state ,choice: integer
Start
state = 1
Repeat
RenderStartingScreen() /* function that draws the interface using writes and reads*/
while(state == 1)
End
And inside of the render function we find

Procedure: RenderStartingScreen
Start
state = 2
Repeat
write(“Welcome . . . “)
read(choice)
switch(choice)
Case 1:
RenderScreen1()
….
Case N:
RenderScreenN()

until( state == 2)
Program:
Part 1: The basics
Here we will look into the equivalent of the algorithms in the C programming language and the actual
applications that will be executed by the computer:

First we start by importing some libraries, the stdio.h library which contains the functions we will use to
print messages onto the terminal, and the stdlib.h library which has the function malloc() which we will use to
allocate memory with.

Then we will declare the some color constants to print colored text which will be much more presentable.

And then define the record structures for the patients, diagnostics, and doctors.

Next we’ll define empty linked lists and the state and choice as global variables.
Part 2: Linked list manipulation

we will present the code for operations on linked lists using only patient examples but note that these can
apply to any of the other systems.

Adding a patient.
void addPatient(){

//allocate memory for patient node


struct patient* newPatient = malloc(sizeof(struct patient));
//declare variable for head pointer
struct patient* curr = patientList;

//assign unique id based on previous id


newPatient->patientID = patientList == NULL? 0: curr->patientID+1;

//assign all values into their respective fields


printf("Enter patient name: ");
scanf(" %[^\n]%*c",newPatient->fullName);
do
{
printf("Enter patient gender (M/F): ");
scanf(" %c",&newPatient->gender);

if (newPatient->gender != 'M' && newPatient->gender != 'F')


{
printf( RED "please enter a valid gender \n\n" RESET);
}

} while (newPatient->gender !='M' && newPatient->gender !='F');

printf("Enter patient height: ");


scanf("%f",&newPatient->height);
printf("Enter patient age: ");
scanf("%i",&newPatient->age);

//insert patient node into the patient list


newPatient->next = curr;
patientList = newPatient;

//flag on success
printf( GREEN "patient added, id: %i\n\n" RESET,newPatient->patientID);
}

Printing a Patient
void printPatient(struct patient* target){

printf("ID: %i, Name: %s, Age: %i, Gender: %c, Height:


%f\n",target->patientID,target->fullName,target->age,target->gender,target->height);

Searching a Patient

struct patient* searchPatient(int ID ){

//declare traversing pointer


struct patient* iterator = patientList;

while (iterator != NULL)


{
//compare with every string until we find one that matches
if(iterator->patientID == ID){
return iterator;
}else{
//advance by one
iterator = iterator->next;
}
}
//return NULL if the node is not found
printf(RED"\n\npatient not found\n\n"RESET);
return NULL;
}

Deleting a Patient
void deletePatient(struct patient* target){
//declare traversing pointers
struct patient* currNode = patientList;
struct patient* nextNode = NULL;

//base case for if the seeked patient is first in the list


if(patientList == target){
patientList = patientList->next;
free(currNode);
printf(GREEN"\n\nPatient deleted successfully\n\n"RESET);
}else{
while (currNode != NULL)
{
//check if the next node is the desired one
if(currNode->next== target){

//assign the next pointer to be 2 steps off the current one


nextNode = currNode->next->next;

//delete the seeked node


free(currNode->next);

//link the now current node to the next node


currNode->next = nextNode;

//set current to NULL to exit out of the loop


currNode = NULL;

printf(GREEN"\n\nDiagnostic deleted successfully\n\n"RESET);


}else{
//advance by one step
currNode = currNode->next;
}
}
}
}

Updating a Patient

void updatePatient(struct patient* target){

//declare traversing pointer


struct patient* iterator = patientList;
int choice;
while (iterator != NULL)
{
//compare with every node until we find one that matches
if(iterator == target){
//print the patient for context
printPatient(target);
//determine what needs to be changed
printf("\n What would you like to update?\n");
printf("1) Name \n2) Age \n3) Gender \n4) Height \n\nChoice: ");
scanf("%d",&choice);

//run a switch statement instead of an if statement for better readability


switch (choice)
{
case 1:
printf("New name: ");

//special conversion specifier to accept input with spaces


scanf(" %[^\n]%*c",iterator->fullName);
break;
case 2:
printf("New age: ");
scanf("%d",&iterator->age);
break;
case 3:
printf("New gender: ");
scanf(" %c",&iterator->gender);
break;
case 4:
printf("New height: ");
scanf("%f",&iterator->height);
break;
default:
break;
}
//flag for success and set iterator to NULL to exit the loop
printf(GREEN"Patient updated sucessfully\n"RESET);
iterator = NULL;
}else{
//advance by one
iterator = iterator->next;
}
}
}
Along with this we have created a variation of the print patient that prints all values

void printAllpatients(){
if (patientList == NULL)
{
printf(RED"\nThere are currently no patients registered\n\n" RESET);
}else{
struct patient* p = patientList;

printf("\n******************************************************************\n");
for (int i = 1; p!= NULL; i++)
{
printf("%i: ",i);
printPatient(p);
p = p->next;
}

printf("\n******************************************************************\n");

And finally we made a function that converts diagnosis and specialty enumerations to strings for printing
messages.

char* DocToStr(int code){


switch (code)
{
case 1:return "Venereologist";
case 2:return "urologist";
case 3:return "gynecologist";
}
}
Part 3: User interface

We will use the a clever technique to create a terminal user interface using a bunch of do while loops and
switch statements, first we start by defining a function that renders the main screen

void RenderStartingScreen(){
state = 1;
printf(BLUE"\nPATIENT DIAGNOSTIC SYSTEM (by Islam)"RESET);
printf("\n\nWhat would you like to manage?.\n\n1)Patients\n2)Diagnoses\n3)Doctors\n4)Exit program\n\n choice: ");
scanf("%i",&choice);

switch (choice)
{
case 1:
RenderPatientScreen();
break;
case 2:
RenderDiagnosticScreen();
break;
case 3:
RenderDoctorScreen();
break;
case 4:
state--;
break;
default:
printf(RED"\n\nPlease enter a valid choice\n\n"RESET);
break;
}
}

And then put it in our main :

// MAIN THREAD
int main(void){
state = 1;
do
{
RenderStartingScreen();
} while (state == 1);
return 0;
}

Notice how this will keep repeating aslong as we don’t pick 4 ( exit ) which will promptly decrease the state
causing us to exit the loop and thus exit the program, however if we pick any other option then we will run into
one of the other rendering functions, let’s take RenderPatientScreen() for example.

void RenderPatientScreen(){
// set to next state
state = 2;
int id;
do
{
printf(BLUE"\nPatient Management:"RESET);
// determine what action needs to be done
printf("\n\n1)Add patient\n2)Update patient\n3)Delete patient\n4)View all
patients\n5)Back\n\nChoice:");
scanf("%i",&choice);
switch (choice)
{
case 1:
addPatient();
break;
case 2:
// unless if the list is empty search for the patient and update it
if(patientList != NULL){
printf("Enter the ID of the patient you want to Update: ");
scanf("%i",&id);
if(searchPatient(id)!=NULL){
updatePatient(searchPatient(id));
}
break;
//otherwise it will not
}else{printf(RED"\nThere are currently no patients registered\n\n" RESET);break;}
case 3:
//will delete the wanted node if the list is not empty
if(patientList!=NULL){
printf("Enter the ID of the patient you want to Delete: ");
scanf("%i",&id);
if(searchPatient(id)!=NULL){
deletePatient(searchPatient(id));
}
break;
// otherwise output a message
}else{printf(RED"\nThere are currently no patients registered\n\n" RESET);break;}
case 4:
printAllpatients();
break;
case 5:
// break out of the loop to return to previous interface
state--;
break;
default:
// error message in unexpected cases
printf(RED"\n\nPlease enter a valid choice\n\n"RESET);
break;
}
} while (state == 2);

Notice how this function will also keep repeating aslong as the state is 2 unless we either input 5 which will
cause us to exit out of this loop back to our original loop (the starting screen) or if input one of the options it
will perform the action and then reprompt our menu.
Part 4: Linking diagnostics

All we have to do here is make an extra field for every doctor record which will be a pointer to a diagnostic
which will serve as the head of a new linked list, we will then also have to add a field for the diagnostics
record to have a pointer for the next element in the list of diagnoses appointed to the doctor.

void addDiagnosticToDoctor(){
state = 3;
int diagid;
int docid;
struct doctor* doctor;
struct diagnostic* diagnostic;
do
{
//prints diagnostics for context
printAllDiagnostics();

//determine the diagnostic we need


printf("Enter the ID of the diagnostic you want to appoint to a doctor: ");
scanf("%i",&diagid);

// if it is not found we display an error message


if (searchDiagnostic(diagid) == NULL && diagnosticList !=NULL)
{
printf(RED"\nDiagnostic does not exist\n"RESET);
}else{
//else we then print all doctors for context
printAllDoctors();

//we get the doctor


printf("Enter the ID of the doctor you want to appoint this diagnosis to:
");
scanf("%i",&docid);

// if we don't find him we print an error message


if (searchDoctor(docid) == NULL && doctorList !=NULL)
{
printf(RED"\nDiagnostic does not exist\n"RESET);
}else{
//else we define our variables for readability
doctor = searchDoctor(docid);
diagnostic = searchDiagnostic(diagid);

// we run a switch statement to figure out which diagnosis is good for


which doctor
switch (diagnostic->Diagnosis)
{
case 1:
case 2:
if (doctor->specialty != 1)
{
printf(RED"\nDoctor is not qualified."RESET);

}else{appendDiagnotic(doctor,diagnostic);}
state--;
break;
case 3:
case 4:
if (doctor->specialty != 2)
{
printf(RED"\nDoctor is not qualified."RESET);

}else{appendDiagnotic(doctor,diagnostic);}
state--;
break;
case 5:
case 6:
if (doctor->specialty != 3)
{
printf(RED"\nDoctor is not qualified."RESET);

}else{appendDiagnotic(doctor,diagnostic);}
state--;
break;

default:
printf(RED"\n\nPlease enter a valid response\n\n"RESET);
break;
}
}
}

} while (state == 3);

In wich we call a function append diagnostic that will simply prepend the node onto the list.

That concludes the program that was to be shown.

Conclusion:
In this problem, we were focused on the developpment of a patient diagnosis system for healthcare and
after careful analysis we used a combination of clever techniques and tools like linked lists and control
structures to eventually materialise it into a working program.

While this program is relatively efficient and gets the job done we also recognize the capacity for
improvements, whether it is using a different control structure or a more optimal algorithm, however this is
what our minds came up with and we have executed it to the best of our abilities.

By contrasting this solution with the works of our peers, we found much more interesting approaches to this
problem and hence by experience we have come to realize the necessity of collaboration.

You might also like