Hello
I use 2 linked list
===> Doctor
===>student
the First doctor A, has three students Student_1, Student_2 and Student_3.
the second doctor B, has one students Student_4.
the Third doctor C, has one students Student_5.
to linked student, i using this function:
Doctor * Doct;
student *st1, *st2;
Doct = new Doctor;
Doct->Do = "A";
st1 = new student;
st1->next = NULL;
st1->St = "Student_1";
st2 = new student;
st2->next = st1;
st2->St = "Student_2";
st1 = st2;
st2 = new student;
st2->next = st1;
ptr_t->add_tasks(Doct);
Doct->Do = "B";
st1 = new student;
st1->next = NULL;
st1->St = "Student_3";
Doct->Stu = st1;
ptr_t->add_tasks(Doct);
Doct->Do = "C";
st1 = new student;
st1->next = NULL;
st1->St = "Student_4";
Doct->Stu = st1;
ptr_t->add_tasks(Doct);
How do I create a function add_student ,Instead of this primitive method .
#include <iostream>
#include <string>
using namespace std;
struct student
{
string St;
student * next;
};
class student_Un
{
private:
student *first;
public:
~student_Un();
student_Un();
void add_student(student *p);
void gener_student();
};
/* ------------------------------------------*/
struct Doctor
{
string Do;
student * Stu;
Doctor * next;
};
class Doctor_Un
{
private:
Doctor *first;
public:
~Doctor_Un();
Doctor_Un();
void add_tasks(Doctor *p);
void gener_tasks();
};
/* ------------------------------------------*/
Doctor_Un :: ~Doctor_Un()
{
typedef struct Doctor t;
t * ptr;
t * qtr;
ptr = first;
qtr = first->next;
while (ptr)
{
delete ptr;
ptr = qtr;
if (! qtr)
{
qtr = qtr->next;
}
}
}
/* ------------------------------------------*/
Doctor_Un ::Doctor_Un()
{
first=NULL;
}
/* ------------------------------------------*/
void Doctor_Un :: add_tasks(Doctor *p)
{
Doctor * ptr;
ptr = new Doctor;
*ptr = *p;
if(first == NULL)
{
ptr->next = NULL;
}
else
{
ptr->next = first;
}
first = ptr;
}
/* ------------------------------------------*/
void Doctor_Un :: gener_tasks()
{
Doctor * ptr;
for(ptr = first; ptr ; ptr = ptr->next)
cout<< ptr->Do << '\t';
cout<<'\n';
}
/* -------------------------------------------- */
void allocate( Doctor_Un * ptr_t)
{
Doctor * Doct;
student *st1, *st2;
Doct = new Doctor;
Doct->Do = "A";
st1 = new student;
st1->next = NULL;
st1->St = "Student_1";
st2 = new student;
st2->next = st1;
st2->St = "Student_2";
st1 = st2;
st2 = new student;
st2->next = st1;
ptr_t->add_tasks(Doct);
Doct->Do = "B";
st1 = new student;
st1->next = NULL;
st1->St = "Student_3";
Doct->Stu = st1;
ptr_t->add_tasks(Doct);
Doct->Do = "C";
st1 = new student;
st1->next = NULL;
st1->St = "Student_4";
Doct->Stu = st1;
ptr_t->add_tasks(Doct);
cout << "************************************************************************** \n";
ptr_t->gener_tasks();
}
/* -------------------------------------------- */
int main(int argc, char** argv)
{
Doctor_Un * ptr_t;
ptr_t = new Doctor_Un;
allocate(ptr_t);
int x;
cin >> x;
return 0;
}