-
September 27th, 2010, 03:33 PM
#1
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;
}
-
September 27th, 2010, 06:30 PM
#2
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.
-
September 27th, 2010, 06:53 PM
#3
Re: Function add linked list
Originally Posted by kempofighter
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?
-
September 27th, 2010, 07:02 PM
#4
Re: Function add linked list
Originally Posted by NewLegend2
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?
-
September 27th, 2010, 07:41 PM
#5
Re: Function add linked list
Originally Posted by kempofighter
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
-
September 28th, 2010, 08:07 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|