Solved

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

Posted on 2004-10-12
7
215 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:Mytix
  • 3
  • 2
7 Comments
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 50 total points
ID: 12295703
Hi Mytix,
There's nothing mystical about what you want to do.

Just do a proper wrapping of your list functionality, like:

typedef struct Node_s {
    struct Node_s* next, *prev;
} Node;

typedef struct ListHead_s {
    Node* head,*tail;
} ListHead;

...and define the appropriate functions.
From a more abstract view, each school contains a set of classes and each class contains a set of students.
Make sure you separate the "technical" aspects from the "business flow" aspects, e.g.:

void student_enroll_class(Student* student,Class* the_class) /* Don't name anything "class". That's calling for trouble... */
...as a "business flow" step. Within that, you'd handle the list
stuff.

Just write a routine for all things a student can do with a class. Then write the routines for what a school can do with its classes.

(That's an OO-like approach, but in this case, it's pretty much feasible)





Cheers!

Stefan
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12297270
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

0
 
LVL 3

Author Comment

by:Mytix
ID: 12470916
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!
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 45

Accepted Solution

by:
Kdo earned 50 total points
ID: 12472506
Hi Mytix,

Still no magic here.  (Though there may be a sorcerer or two at work.  :)  )  Break the problem down to it's components.

Let's add student "Jennifer Smith" to "Biology 101".  (Jennifer is much more interesting than John.)

First we find the pointer to the Biology class.

class_t  *ClassList;
class_t  *CurrentClass;
student_t  *CurrentStudent;

  CurrentClass = FindClass (ClassList, "Biology 101");
  if (CurrentClass == NULL)  /*  Class not found  */
    fprintf (stderr, "Class not defined.\n");
  else
  {
    CurrentStudent = FindStudent (CurrentClass->StudentList, "Jennifer Smith");
    if (CurrentStudent == NULL)
      fprintf (stderr, "%s is not registered in this class\n", "Jennifer Smith");
    else
      /*  Student found in this class  */
  }


Good Luck,
Kent
0
 
LVL 3

Author Comment

by:Mytix
ID: 12479987
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
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12482477


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
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now