Function add linked list
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Function add linked list

  1. #1
    Join Date
    Aug 2010
    Posts
    12

    Function add linked list

    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:

    Code:
        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 .

    Code:
    #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;
    }

  2. #2
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Function add linked list

    Sorry but i can't figure out what you are trying to do. The whole thing is primitive. Why not use std::list and not worry about the design of the linked list itself?
    http://cplusplus.com/reference/stl/list/

    if you really want to make your own singly linked list I suggest that you create a really simple template class that works with integers first and get that working. Once you have a template class in place it ought to work with any other type. I also suggest that you do a web search. There are many linked list examples. The code that you posted is very confusing.

  3. #3
    Join Date
    Aug 2010
    Posts
    12

    Cool Re: Function add linked list

    Quote Originally Posted by kempofighter View Post
    Sorry but i can't figure out what you are trying to do. The whole thing is primitive. Why not use std::list and not worry about the design of the linked list itself?
    http://cplusplus.com/reference/stl/list/

    if you really want to make your own singly linked list I suggest that you create a really simple template class that works with integers first and get that working. Once you have a template class in place it ought to work with any other type. I also suggest that you do a web search. There are many linked list examples. The code that you posted is very confusing.
    when I put second Doctor, Students take from the first Doctor
    as,

    Doctor 1
    ===> Student 1
    ===> Student 2

    Doctor 2
    ===> Student 1
    ===> Student 2

    ===> Student 3
    ===> Student 4

    Now, How do I create a function add_student?

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Question Re: Function add linked list

    Quote Originally Posted by NewLegend2 View Post
    when I put second Doctor, Students take from the first Doctor
    as,

    Doctor 1
    ===> Student 1
    ===> Student 2

    Doctor 2
    ===> Student 1
    ===> Student 2

    ===> Student 3
    ===> Student 4

    Now, How do I create a function add_student?
    I have no idea what that first statement means. Again - please state your requirements more clearly. What do you want a linked list of? students? Doctors? Both?

  5. #5
    Join Date
    Aug 2010
    Posts
    12

    Re: Function add linked list

    Quote Originally Posted by kempofighter View Post
    I have no idea what that first statement means. Again - please state your requirements more clearly. What do you want a linked list of? students? Doctors? Both?
    I used 2 linked list.

    First : Students.
    Second: Doctor.

    Every Doctor takes a set of his students.
    as,
    Doctor 1
    ===> Student 1
    ===> Student 2

    Doctor 2
    ===> Student 3
    ===> Student 4



    In this code:
    when I put second Doctor, Students take from the first Doctor
    as,
    Doctor 1
    ===> Student 1
    ===> Student 2

    Doctor 2
    ===> Student 1
    ===> Student 2

    ===> Student 3
    ===> Student 4

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,060

    Re: Function add linked list

    [ removed duplicate thread ]

    Please do not start multiple threads on the same topic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center