How to iterate through linked list elements
<p>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. </p>

<p>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.</p>

<p>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:</p>

<pre><code>#include&lt;iostream&gt;
using namespace std;

template&lt;class T&gt;
class node
{
public:
    T data;
    node&lt;T&gt; *link;
};

template&lt;class T&gt;
class list
{
private:
    node&lt;T&gt; *first;
public:
    list()
    {
            first=NULL;
    }
    ~list()
    {
            node&lt;T&gt; *next;
            while(first)
            {
                    next=first-&gt;link;
                    delete first;
                    first=next;
            }
    }
    void insertion(T insert)
    {
            node&lt;T&gt; *cur,*ptr;
            ptr=first;
            cur=new node&lt;T&gt;;
            cur-&gt;data=insert;
            //cur-&gt;link=NULL;
            cur-&gt;link=first;
            first=cur;
    }
    void display()
    {
            node&lt;T&gt; *ptr;
            if(first==NULL)
            {
                    cout&lt;&lt;"List is empty"&lt;&lt;endl;
            }
            else
            {
                    ptr=first;
                    while(ptr!=NULL)
                    {
                            cout&lt;&lt;ptr-&gt;data&lt;&lt;endl&lt;&lt;endl;
                            ptr=ptr-&gt;link;
                    }
            }
    }
};

class visit
{
private:
    char date[100];
    char doctor[100];
    bool filled;
public:
    healthrecord()
    {
            filled=false;
    }
    void FillVisit()
    {
            char temp[100];
            cout&lt;&lt;"Enter date: ";
            cin&gt;&gt;temp;
            strcpy(date,temp);
            fflush(stdin);
            cout&lt;&lt;"Enter doctor: ";
            cin&gt;&gt;temp;
            strcpy(doctor,temp);
            fflush(stdin);
            filled=true;
    }
    void PrintVisit()
    {
            if(filled)
            {
                    cout&lt;&lt;"PATIENT VISIT"&lt;&lt;endl;
                    cout&lt;&lt;"Date: "&lt;&lt;date&lt;&lt;endl;
                    cout&lt;&lt;"Doctor: "&lt;&lt;illness&lt;&lt;endl&lt;&lt;endl;
            }
    }
    char*GetDoctorName()
    {
            return doctor;
    }
};

ostream&amp; operator&lt;&lt;(ostream&amp; os,visit &amp;object)
{
    object.PrintVisit();
    os&lt;&lt;"";
    return os;
}

class patient
{
private:
    char name[100];
    int age;
    char gender[100];
    list&lt;visit&gt; visits;
public:
    patient(){}
    void FillPatientRecords()
    {
            char temp[100];
            int tmp;
            cout&lt;&lt;"Enter patient's name: ";
            cin&gt;&gt;temp;
            strcpy(name,temp);
            fflush(stdin);
            cout&lt;&lt;"Enter patient's age: ";
            cin&gt;&gt;tmp;
            age=tmp;
            fflush(stdin);
            cout&lt;&lt;"Enter patient's gender: ";
            cin&gt;&gt;temp;
            strcpy(gender,temp);
            fflush(stdin);
    }
    void PrintPatientRecords()
    {
            cout&lt;&lt;endl&lt;&lt;"Name: "&lt;&lt;name&lt;&lt;endl;
            cout&lt;&lt;"Age: "&lt;&lt;age&lt;&lt;endl;
            cout&lt;&lt;"Gender: "&lt;&lt;gender&lt;&lt;endl;
            healthrecords.display();
    }
    char*GetPatientName()
    {
            return name;
    }
    void AddVisit()
    {
        healthrecord temp;
        temp.FillHealthRecord();
        healthrecords.insertion(temp);
    }
};

ostream&amp; operator&lt;&lt;(ostream&amp; os,patient &amp;object)
{
    object.PrintPatientRecords();
    os&lt;&lt;"";
    return os;
}

int main()
{
    list&lt;patient&gt; patients;
    char src[100];
    int ch=0;
    while(ch!=8)
    {
            cout&lt;&lt;".:Menu:."&lt;&lt;endl&lt;&lt;"1. Add new patient"&lt;&lt;endl&lt;&lt;"2. Add new visit"&lt;&lt;endl&lt;&lt;"3. Delete a visit"&lt;&lt;endl&lt;&lt;"4. Delete a patient"&lt;&lt;endl;
            cout&lt;&lt;"5. Display all patients"&lt;&lt;endl&lt;&lt;"6. Search patients"&lt;&lt;endl&lt;&lt;"7. Search visit"&lt;&lt;endl&lt;&lt;"8. Exit"&lt;&lt;endl&lt;&lt;"Enter a choice: ";
            cin&gt;&gt;ch;
            if(ch==1)
            {
                    //add patient data
                    patient temp;
                    temp.FillPatientRecords();
                    patients.insertion(temp);
            }
            else if(ch==2)
            {
                    //add new visit
                    cout&lt;&lt;"Enter patient's name: ";
                    cin&gt;&gt;src;
                    patient temp;
            }
            else if(ch==3)
            {
                    //delete a visit
                    cout&lt;&lt;"Enter patient's name: ";
                    cin&gt;&gt;src;
            }
            else if(ch==4)
            {
                    //delete a patient
                    cout&lt;&lt;"Enter patient's name: ";
                    cin&gt;&gt;src;
            }
            else if(ch==5)
            {
                    //display all patients
                    patients.display();
            }
            else if(ch==6)
            {
                    //search patients
                    cout&lt;&lt;"Enter patient's name: ";
                    cin&gt;&gt;src;
            }
            else if(ch==7)
            {
                    //search visits (print all matching ones)
                    cout&lt;&lt;"Enter doctor's name: ";
                    cin&gt;&gt;src;
            }
            cout&lt;&lt;endl;
    }
    return 0;
}
</code></pre>

<p>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:</p>

<pre><code>patient temp=(element of linked list)
if(temp.GetPatientName()==src)
...
</code></pre>

<p>How do I iterate through each patient in the linked list so I can access their names?</p>