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;
}

In your struct Doctor, you have a declaration

sting Do;

I'm pretty sure you can't do that. Do is a reserved word.

As for your add_student function, I'm assuming you want to add the students onto your doctors, and each doctor will be the start of a linked list. It looks like you are actually creating and adding the students in the allocate function.

void allocate( Doctor_Un * ptr_t){
     Doctor * Doct;
     student *st1, *st2;
     Doct = new Doctor;

     Doct->name = "A";
     add_student(Doct, st1); //this will add st1 onto the Doct struct.
/*   st1 = new student;
     st1->next = NULL;
     st1->St = "Student_1"; */
     st2 = new student;
     add_student(Doct, st2); //This will add st2 onto the end of st1
/*   st2->next = st1;        
     st2->St = "Student_2"; */
     ptr_t->add_tasks(Doct);

The add_student should take in the student along with the doctor you want that student to be given to.

Doct->name = "B";
     st1 = new student;
     st1->next = NULL;
     st1->St = "Student_3";
     Doct->Stu = st1;
     ptr_t->add_tasks(Doct);

     Doct->name = "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();
}

This part, I'm not entirly sure it will work the way you think it will. Doct is still pointing to the original memory location, so it's going to change Doct A to Doct B ie, it will overwrite your first doctor. My suggestion would be instead of trying to create 3 doctors and multiple students in one function, try making an add_doct function and the add_student function and write that into allocate. It might make it easier to understand and write.

void allocate(){
     Doctor * Doct;
     student *st1, *st2;
     student_Un *list1;
     list1 = new student_Un;
     if (first == NULL)
         first = new Doctor;
     else
         add_tasks(first);

     first->name = "A"; //first is now essentially an empty doctor
     list1.add_student(first); //this will add a student onto the Doct struct.
     list1.add_student(first); //This will add another onto the end of the first student

     add_tasks(first); //adds another doctor, makes the new first an empty doctor template

     first->name = "B"; //sets the new first to B
     list1.add_student(first); //adds a student onto the new first
     add_tasks(first); //adds a new doctor, first is now a clean template

     first->name = "C";
     list1.add_student(first);
     cout << "************************************************************************** \n"; 
     gener_tasks();
}

Hopefully I got the syntax correct, but I'm just a little confused on some of the logic applied in this program.

Why not put add_student inside the doctor_Un class and completly eliminate the need for the student_Un class? What kind of errors do you get when you compile this code? Why is your function call in main to allocate not ptr_t.allocate(); ?

void allocate(){
     Doctor * Doct;
     student *st1, *st2;
     student_Un *list1;
     list1 = new student_Un;
     if (first == NULL)
         first = new Doctor;
     else
         add_tasks(first);

     first->name = "A"; //first is now essentially an empty doctor
     list1.add_student(first); //this will add a student onto the Doct struct.
     list1.add_student(first); //This will add another onto the end of the first student

     add_tasks(first); //adds another doctor, makes the new first an empty doctor template

     first->name = "B"; //sets the new first to B
     list1.add_student(first); //adds a student onto the new first
     add_tasks(first); //adds a new doctor, first is now a clean template

     first->name = "C";
     list1.add_student(first);
     cout << "************************************************************************** \n"; 
     gener_tasks();
}

Where was added to the student?
as,

st1->St = "Student_1";

I did not understand what to put first?

list1.add_student(first);

Can you copy all the code?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

void allocate(){
     Doctor * Doct;
     student *st1, *st2;
     student_Un *list1;
     list1 = new student_Un;
     if (first == NULL)
         first = new Doctor;
     else
         add_tasks(first);

     first->name = "A"; //first is now essentially an empty doctor
     list1.add_student(first); //this will add a student onto the Doct struct.
     list1.add_student(first); //This will add another onto the end of the first student

     add_tasks(first); //adds another doctor, makes the new first an empty doctor template

     first->name = "B"; //sets the new first to B
     list1.add_student(first); //adds a student onto the new first
     add_tasks(first); //adds a new doctor, first is now a clean template

     first->name = "C";
     list1.add_student(first);
     cout << "************************************************************************** \n"; 
     gener_tasks();
}

Where was added to the student?
as,

st1->St = "Student_1";

I did not understand what to put first?

list1.add_student(first);

Can you copy all the code?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.