In: Computer Science
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List.
Each patient's record or node should have the following data:
Patient Name:
Age:
Last 4 digits of Social Security Number:
The program should first display a menu that gives the user the option to:
1) Add a Patient's record (After adding one patient the user should be prompted: Do you want to add another patient's record? 1 for Yes and 0 for No)
2) Modify a Patient's record ((After modifying/updating one patient the user should be prompted: Do you want to add modify patient's record? 1 for Yes and 0 for No)
3) Delete a Patient's Record ((After deleting one patient the user should be prompted: Do you want to delete another patient's record? 1 for Yes and 0 for No)
4) Display a Patient's record
/* You can compile and run here https://www.onlinegdb.com/online_c++_compiler , all functions are according to the specification provided */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int ssn;
int age;
string name;
struct node *next;
}*start;
/*
* Creating Node
*/
node *create_node(int value,int value1,string name1)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->ssn = value;
temp->age=value1;
temp->name=name1;
temp->next = NULL;
return temp;
}
}
/*
* Inserting Node
*/
void insert_last()
{
int value,value1;string name1;
cout<<"Enter Name: ";
cin>>name1;
cout<<"Enter ssn: ";
cin>>value;
cout<<"Enter age: ";
cin>>value1;
struct node *temp, *p;
temp = create_node(value,value1,name1);
if (start == NULL)
{
start = temp;
start->next = NULL;
cout<<"Data Inserted\nDo You Want to add another patient‘s
record? (1 for Yes and O for No)";
int ch;
cin>>ch;
if(ch==1)
insert_last();
}
else
{
p = start;
start = temp;
start->next = p;
cout<<"Data Inserted\nDo You Want to add another patient‘s
record? (1 for Yes and O for No)";
int ch;
cin>>ch;
if(ch==1)
insert_last();
}
}
/*
* Sorting Link List
*/
void sort()
{
struct node *ptr, *s;
int value;
if (start == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
ptr = start;
while (ptr != NULL)
{
for (s = ptr->next;s !=NULL;s = s->next)
{
if (ptr->ssn > s->ssn)
{
value = ptr->ssn;
ptr->ssn = s->ssn;
s->ssn = value;
}
}
ptr = ptr->next;
}
cout<<"The List have been sorted according to the SSN";
}
/*
* Delete element at a given position
*/
void delete_pos()
{ int value, pos = 0,counter=0,i;
bool flag = false;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the SSN to be deleted: ";
cin>>value;
struct node *s,*ptr;
s = start;
while (s != NULL)
{
pos++;
if (s->ssn == value)
{
flag = true;
break;
}
s = s->next;
}
if (!flag)
cout<<"Patient SSN"<<value<<" not found in the
list"<<endl;
else{s=start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Data Deleted\nDo You Want to delete another patient‘s
record? (1 for Yes and O for No)";
int ch;
cin>>ch;
if(ch==1)
delete_pos();
}
}
}
/*
* Update a given Node
*/
void update()
{ int value,value1, pos = 0;string name1;
bool flag = false;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the SSN to be updated: ";
cin>>value;
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->ssn == value)
{ cout<<"Enter name: ";
cin>>name1;
cout<<"Enter age: ";
cin>>value;
cout<<"Enter ssn: ";
cin>>value1;
s->ssn=value1;
s->age=value;
s->name=name1;
cout<<"Data Updated";
flag = true;
break;
}
s = s->next;
}
if (!flag){
cout<<"Patient SSN"<<value<<" not found in the
list"<<endl;
cout<<"\nDo You Want to update another patient‘s record? (1
for Yes and O for No)";
int ch;
cin>>ch;
if(ch==1)
update();
}else{
cout<<"\nDo You Want to update another patient‘s record? (1
for Yes and O for No)";
int ch;
cin>>ch;
if(ch==1)
update();
}
}
/*
* Searching an element
*/
void search()
{
int value, pos = 0;
bool flag = false;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the SSN to be searched: ";
cin>>value;
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->ssn == value)
{
flag = true;
cout<<"Data Found\nPatient SSN = "<<value<<" Name
= "<<s->name<<" Age =
"<<s->age<<endl;
}
s = s->next;
}
if (!flag)
cout<<"Patient SSN"<<value<<" not found in the
list"<<endl;
}
/*
* Display Elements of a link list
*/
void display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<"SSN = "<<temp->ssn<<" Name =
"<<temp->name<<" Age =
"<<temp->age<<"\n";
temp = temp->next;
}
}
/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Patient's data singly linked
list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Patient's Data"<<endl;
cout<<"2.Sort Data by SSN"<<endl;
cout<<"3.Delete Patient's Data By SSN"<<endl;
cout<<"4.Update Patient's Data"<<endl;
cout<<"5.Search SSN"<<endl;
cout<<"6.Display Data"<<endl;
cout<<"7.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node : "<<endl;
insert_last();
cout<<endl;
break;
case 2:
cout<<"Sort Link List By SSN: "<<endl;
sort();
cout<<endl;
break;
case 3:
cout<<"Delete a particular node by SSN: "<<endl;
delete_pos();
break;
case 4:
cout<<"Update Node Value:"<<endl;
update();
cout<<endl;
break;
case 5:
cout<<"Search element in Link List: "<<endl;
search();
cout<<endl;
break;
case 6:
cout<<"Display elements of link list"<<endl;
display();
cout<<endl;
break;
case 7:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}