Hello
I used 2 linked list.
First : Students.
Second: Doctor.
Every Doctor takes a set of his students.
The problem :
How do I add students to the doctor?
#include <iostream>
using namespace std;
#include <string>
struct student
{
string name; // current name
student *next; // pointer to next student
} ;
class student_Univ
{
private:
student *head; // pointer to head
student *curr; // pointer to current
public:
student_Univ(); // constructor
void ClearLinkedList(); // clear list
void add_stu(student *p); // add student
void print_stu(); // print all in list
};
struct Doctor
{
string name; // current name
student_Univ StName;
Doctor *next; // pointer to next student
} ;
class Doctor_Univ
{
private:
Doctor *head; // pointer to head
Doctor *curr; // pointer to current
public:
Doctor_Univ(); // constructor
void add_doc(Doctor *p); // add student
void print_doc(); // print all in list
};
// constructor
student_Univ::student_Univ()
{
head = NULL; // head points to NULL
curr = NULL; // current points to NULL
}
Doctor_Univ::Doctor_Univ()
{
head = NULL; // head points to NULL
curr = NULL; // current points to NULL
}
// add student
void student_Univ::add_stu(student *p)
{
if(head == NULL) // first in list
{
head = p; // head
curr = head; // current points to head
}
else
{
curr->next = p; // add to list
curr = p; // update current pointer
}
}
void Doctor_Univ::add_doc(Doctor *p)
{
if(head == NULL) // first in list
{
head = p; // head
curr = head; // current points to head
}
else
{
curr->next = p; // add to list
curr = p; // update current pointer
}
}
// print all in list
void student_Univ::print_stu()
{
student *ptr = head;
cout << "*********************" << endl;
cout << "student is: " << endl;
// for each in list, print out all
while(ptr != NULL)
{
cout << ptr->name << endl;
ptr = ptr->next; // iterate to next student in list
}
cout << "*********************" << endl;
}
void Doctor_Univ::print_doc()
{
Doctor *ptr = head;
cout << "*********************" << endl;
cout << "doctor is: " << endl;
// for each in list, print out all
while(ptr != NULL)
{
cout << ptr->name << endl;
ptr = ptr->next; // iterate to next student in list
}
cout << "*********************" << endl;
}
int main()
{
student_Univ *ptr_ie = new student_Univ();
Doctor_Univ *ptr_doc = new Doctor_Univ();
// define student 1
student *ine = new student;
ine->name = "AAA";
ine->next = NULL;
ptr_ie->add_stu(ine);
// define student 2
ine = new student;
ine->name = "BBB";
ine->next = NULL;
ptr_ie->add_stu(ine);
// define student 3
ine = new student;
ine->name = "CCC";
ine->next = NULL;
ptr_ie->add_stu(ine);
// print out list
ptr_ie->print_stu();
/////////////////////////////////////////
/////////////////////////////////////////
Doctor *doct = new Doctor;
doct->name = "Jon";
//doct->StName = ptr_ie; <== How Can Input set of student ???
doct->next = NULL;
ptr_doc->add_doc(doct);
ptr_doc->print_doc();
return 0;
}