Link to home
Start Free TrialLog in
Avatar of Mytix
Mytix

asked on

simple example of a linked list in a linked list in C

Hi, i have a small question. Can someone give me an example of a linked list in a linked list? so for example the data are classes of students; the first linked list are classes and in each class is a linked list of students. also how do I add/delete/manipulate linked list in linked list?
thanks!
PS: just a small example would do
SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kent Olsen
Hi Mytix,

Stefan's correct -- there is no magic here.  (Though the concept may be tough to visualize until you've actually used it.)

Break the problem into the sum of its parts.  You've got a structure that defines a class:


typedef struct
{
  int   ClassNumber;
  int   ClassStartTime;
/*  etc  */
  class_t *Next;
  student_t *StudentList;
} class_t;

and a structure that defines a student:

typedef student
{
  int   StudentNumber;
/*  etc  */
  student_t *Next;
};

Tie it all together with a starting point for the class list:

class_t  *ClassList;

Now when you build the structures, you generate data that (conceptually) looks like this:

Class1  ->  Student1  ->  Student2  ->  Student3 ->  ....
   |
   \/
Class2  ->  Student1  ->  Student4  ->  Student5 ->  ....
   |
   \/
Class3  ->  Student4  ->  Student 6 ->  Student 7 -> ...
   |
   \/
...

Your "primary" linked list is class, and within each class you have a list of students.  The thing to remember is that when you build your functions to manage these lists, you build a set of functions to manage classes and another to manage students.  Within the class functions you reference the student lists only when you need access to student data.  Within the student functions, you should never have to reference the class lists.  (Of course, if/when the student data contains the students "list" of classes, this may change.)


Good Luck,
Kent

Avatar of Mytix
Mytix

ASKER

thanks for the replies, but i still am stuck (and recovering from my illness)
how do i actually go through the linked list of linked list and add data to them? and do manipulation to these linked list of linked list.
thanks!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mytix

ASKER

thats strange, im still quite perplexed:
are the functions FinClass and FindStudent built-in? or must i make those functions?
im thinking of sorting the classes and students; students first then classes, but still cant get around the basic sorting of linked list, much more linked list of linked lists.
thanks for your help


You're going to have to build these functions yourself.  The standard C libraries provide you with very few "built in" functions for handling linked lists.

Start easy and build up.

Write your AddClass (), ShowClasses(), and FindClass () functions.  Once you've built the linked list of classes we can move on to building the list of lists.


Kent