Thread: Link Lists of Structures within Structures...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    16

    Link Lists of Structures within Structures...

    Sorry if the title was confusing ( im a little confused! ) Was wondering if anyone know how to dynamically set up a link list in C. Basically i have a structure (students) which have two data pieces, a int which is the id # for that student and then they need to have a linked list of the courses they are taking (courses are the second struct) in the course struct it has the number of credits, max students, time etc that kind of stuff. But what im wondering is how I could dynamically make a link list for each student that would have the correct number of courses, because each student could have any number of classes I don't know how to do this. I could do it if each student only had one class ^^, thanks for any info on the subject.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Basically, each student struct would have a next pointer to point to the next student (or 0 (NULL) for the end) and would also have a pointer to the first class struct. The class struct would have a pointer to the next class. Like this:
    Code:
    S -> S -> S -> 0
    |    |    |
    C    C    C
    |    |    |
    C    0    C
    |         |
    0         C
              |
              0
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    okay so im just fuzzy on a few things currently I have which i THINK is correct, but so I have this struct setup, how can I add this to my student struct? and when running the program what would the syntax be for setting the next within this class struct that is within the student struct? Thanks for your help so far! it rocked
    Code:
    typdef struct Class
    {
    int classId;
    int section;
    int credits;
    Struct Class * next;
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Except for the fact that struct should be all lowercase, yes, that's the right idea. I prefer using a typedef so I don't have to keep retyping struct. I put it above the actual struct so I can use it inside the struct too (just don't typedef any pointers -- it makes the code confusing):
    Code:
    typedef struct Class     Class;
    struct Class {
        int id;
        int section;
        int credits;
        Class *next;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to link 2 structures
    By sivapc in forum C++ Programming
    Replies: 26
    Last Post: 09-28-2009, 12:17 AM
  2. C Linked Lists and Structures help!
    By Yorae in forum C Programming
    Replies: 0
    Last Post: 03-07-2006, 09:55 AM
  3. Can i do this: Structures and linked lists help
    By satory in forum C Programming
    Replies: 4
    Last Post: 04-25-2005, 07:49 AM
  4. help on linked lists(structures)
    By dionys in forum C Programming
    Replies: 3
    Last Post: 06-01-2004, 08:57 AM
  5. C Structures and Linked Lists.
    By bobthebuilder20 in forum C Programming
    Replies: 14
    Last Post: 04-15-2003, 05:00 PM