I'm trying to solve a homework problem for my data structure class. We're supposed to make a list of hospital patients, each patient has a name, age, gender, and a list of visits. Each visit has a date and a doctor.
The program should be able to add new patients, then separately add new visits into a patient's record by entering the patient's name (the problem assumes every patients' names are unique). The program should also be able to delete patients, delete visits, search patients by name, search visits by doctors, and display all patient data.
For the linked list, we're supposed to make our own linear singly linked list with a node class (not allowed to use existing data structures). My full code is the following:
#include<iostream>
using namespace std;
template<class T>
class node
{
public:
T data;
node<T> *link;
};
template<class T>
class list
{
private:
node<T> *first;
public:
list()
{
first=NULL;
}
~list()
{
node<T> *next;
while(first)
{
next=first->link;
delete first;
first=next;
}
}
void insertion(T insert)
{
node<T> *cur,*ptr;
ptr=first;
cur=new node<T>;
cur->data=insert;
//cur->link=NULL;
cur->link=first;
first=cur;
}
void display()
{
node<T> *ptr;
if(first==NULL)
{
cout<<"List is empty"<<endl;
}
else
{
ptr=first;
while(ptr!=NULL)
{
cout<<ptr->data<<endl<<endl;
ptr=ptr->link;
}
}
}
};
class visit
{
private:
char date[100];
char doctor[100];
bool filled;
public:
healthrecord()
{
filled=false;
}
void FillVisit()
{
char temp[100];
cout<<"Enter date: ";
cin>>temp;
strcpy(date,temp);
fflush(stdin);
cout<<"Enter doctor: ";
cin>>temp;
strcpy(doctor,temp);
fflush(stdin);
filled=true;
}
void PrintVisit()
{
if(filled)
{
cout<<"PATIENT VISIT"<<endl;
cout<<"Date: "<<date<<endl;
cout<<"Doctor: "<<illness<<endl<<endl;
}
}
char*GetDoctorName()
{
return doctor;
}
};
ostream& operator<<(ostream& os,visit &object)
{
object.PrintVisit();
os<<"";
return os;
}
class patient
{
private:
char name[100];
int age;
char gender[100];
list<visit> visits;
public:
patient(){}
void FillPatientRecords()
{
char temp[100];
int tmp;
cout<<"Enter patient's name: ";
cin>>temp;
strcpy(name,temp);
fflush(stdin);
cout<<"Enter patient's age: ";
cin>>tmp;
age=tmp;
fflush(stdin);
cout<<"Enter patient's gender: ";
cin>>temp;
strcpy(gender,temp);
fflush(stdin);
}
void PrintPatientRecords()
{
cout<<endl<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
healthrecords.display();
}
char*GetPatientName()
{
return name;
}
void AddVisit()
{
healthrecord temp;
temp.FillHealthRecord();
healthrecords.insertion(temp);
}
};
ostream& operator<<(ostream& os,patient &object)
{
object.PrintPatientRecords();
os<<"";
return os;
}
int main()
{
list<patient> patients;
char src[100];
int ch=0;
while(ch!=8)
{
cout<<".:Menu:."<<endl<<"1. Add new patient"<<endl<<"2. Add new visit"<<endl<<"3. Delete a visit"<<endl<<"4. Delete a patient"<<endl;
cout<<"5. Display all patients"<<endl<<"6. Search patients"<<endl<<"7. Search visit"<<endl<<"8. Exit"<<endl<<"Enter a choice: ";
cin>>ch;
if(ch==1)
{
//add patient data
patient temp;
temp.FillPatientRecords();
patients.insertion(temp);
}
else if(ch==2)
{
//add new visit
cout<<"Enter patient's name: ";
cin>>src;
patient temp;
}
else if(ch==3)
{
//delete a visit
cout<<"Enter patient's name: ";
cin>>src;
}
else if(ch==4)
{
//delete a patient
cout<<"Enter patient's name: ";
cin>>src;
}
else if(ch==5)
{
//display all patients
patients.display();
}
else if(ch==6)
{
//search patients
cout<<"Enter patient's name: ";
cin>>src;
}
else if(ch==7)
{
//search visits (print all matching ones)
cout<<"Enter doctor's name: ";
cin>>src;
}
cout<<endl;
}
return 0;
}
This works just fine if all I did was add new patients. However, I have no idea how to add new visits to a patient, since I'll have to search a patient's name and add the new visit to that patient. The search method I have in mind is to iterate through each patient in the list in main(), then compare it to the search query like so:
patient temp=(element of linked list)
if(temp.GetPatientName()==src)
...
How do I iterate through each patient in the linked list so I can access their names?