Question

In: Computer Science

Using C++, Create a singly Linked List of patients list so that you can sort and...

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

Solutions

Expert Solution

/* 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;
}
}
}


Related Solutions

Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent...
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. • in the term 4X2, the coefficient is 4 and the exponent 2 • in -6X8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The school has multiple classes. Each class has a different number of students. class student { Long ID; string Name; string Address; float grades[3]; student *below; }; class Node // the node represents a class in school { int ID; int NoOfStudents; int NoOfQuizes; student *t;// a linked list of students is allocated dynamically Node *Next; }; class school { string Name; Node *Head; int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT