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

Recommended Answers

All 2 Replies

How do I add students to the doctor?

Well, just uncomment the line 153 you have and it should work. Doesn't it? I would say that you need to implement a deep copy-constructor for class student_Univ, but, since your management of memory, pointers, and ownership is essentially non-existent in your code (which is very bad, especially when working with linked-lists), I guess you have to stick with the shallow copy-constructor that the compiler provides.

Two remarks:
1) You have a ton of memory leaks (not dangerous ones, but still). You should google for the topic of memory leaks and revise your code (essentially you need to add clean-up code for all that dynamic memory you are allocating). You need to better understand pointers, dynamic memory, and ownership issues.
2) Hint: classes student_Univ and doctor_Univ are not needed. This is a common mistake when making a simple linked-list. Except for a few special purposes, decentralized (recursive) algorithms, as part of classes student or Doctor, can pretty much do everything you need the linked-list for, and thus, no "managing" or centralized classes or algorithms are needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.