1

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?

Stella
  • 95
  • 3
  • 13
  • 1
    Not knowing the rest of the meat in your `list` template (seriously poor choice of names there, btw, particularly if you used `using namespace std;`, [which you shouldn't do](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) but apparently are), we have to assume you have provided a way to reference a patient (singluar) in your patient list by the results of your search. Given that reference, managing/accessing that patient's list of visits should be achievable via methods exposed in your patient class. In short, you have to code it eventually. – WhozCraig Oct 31 '16 at 07:57
  • @WhozCraig You could use a little less sass in your answer; some people are new to programming. – gowrath Oct 31 '16 at 08:03
  • @WhozCraig But there's the catch: in order to search a patient by name, I think I'll need to access each patient's name from within the list object in the main() and then compare it with the user's search query in order to find a match. How do I access each patient's name from within the list object? – Stella Oct 31 '16 at 08:05
  • @Penny You write public methods in the class that let external users get that information about that instance of the class. See the getName() method in my answer. – gowrath Oct 31 '16 at 08:06
  • @gowrath I've edited my question to include my full code. If I want to add a new visit to a patient, I suppose I'll have to iterate through each patient in the list and compare their names to the search query, but how can I do that in main()? What I have in mind is `patient &cur = (each element of patients list)`, then something like `if(src==cur.GetPatientName()) { (do something) }`. How do I iterate through each patient in the list? – Stella Oct 31 '16 at 08:39
  • @Penny You would have to make a `getFirst()` method in the list class that will return the front of the list. Then you can iterate with: `node *curr = getFirst()`, followed by `while(curr != NULL) { /*... do things ... */; curr = curr->link}`. See my answer edit. – gowrath Oct 31 '16 at 09:13

1 Answers1

0

As your code stands, you can't change the visits for a patient because the visits list is a private member of the patient class. If it were public, then you could do something like

list<visit> &visits = patients.front().visits
visits.push_back(/* some new visit */);

Following that, it may be tempting to just make the visits list public. However, note that this is considered bad style (and functionality) in OOP. Instead you should write mutator methods. That is, provide a public method in the patient class that takes a dat and a doctor as a parameter and in it, will create a new visit and append it to the visits list of the patient you called it one. Something like the addVisit(..) method below. Similarly if you want people to be able to access the name of the patient, you can write a public getName() method that will return the name of the patient:

class patient
{
public:
    void addVisit(string date, string doctor) {
        visit v(date, doctor);
        visits.push_back(v);
    }

    string getName() {        // public method used to get name
        return name;
    }

private:
    string name;
    int age;
    string gender;
    list<visit> visits;
...

So to use this for example, you could do:

int main() {
    list<patient> patients;
    // ... populate patients list

    patient &curr = patients.front();        // say you want to add a visit for the first patient in the list
    curr.addVisit("25 Sep", "Dr. Harambe");

}

I may have messed up some syntax and other things here (half wrote this on a phone), but the gist of it is what matters.

Post question edit answer:

To iterate through the list, write a method that returns the front of the list and use that as such:

node *curr = patients.getFront();
while(curr != NULL) {
    if(curr.getName() == src) {
        //... do stuff
    curr = curr->link;
}
gowrath
  • 3,136
  • 2
  • 17
  • 32