Linked Lists..

This is a discussion on Linked Lists.. within the C Programming forums, part of the General Programming Boards category; I have a struct/union as follows Code: typedef struct _person{ struct _person *next; struct _person *prev; struct _grades *grade_sent; double ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Linked Lists..

    I have a struct/union as follows
    Code:
    typedef struct _person{
    struct _person *next;
    struct _person *prev;
    struct _grades *grade_sent;
    double average;
    } Person;
    
    tyepdef struct _grades{
        struct _grades *next;
        struct _grades *prev;
        union{
            struct{
                int num_fail;
                double grade;
            } Fails;
            struct{
                int num_pass;
                double grade;
            } Pass;
        } grade_specifics;
    } Grades;
    so basically, its a list.. with lists of their own on each node...

    I have this declaration of my list:
    Code:
    Person *list;
    I can send in this list to an add_node function and it works fine:
    Code:
    Person add_node(Person *sent)
    {
        Person *new_node;
        new_node = (Person *)malloc(sizeof(Person));
        sent->prev->next = new_node;
        new_node->prev = sent->prev;
        new_node->next = sent;
        sent->prev = new_node;
    
    //Here I attempt to initialize the grades list.. but it may not be working right..
        new_node->grade_sent = (Grades *)malloc(sizeof(Grades));
        new_node->grade_sent->next = new_node->grade_sent;
        new_node->grade_sent->prev = new_node->grade_sent;
    }

    I use this function call in my main:
    add_node(list);

    this works..

    How do I add a new grades node on a specific Person?

    I tried something similar using a function like
    Code:
    Grades add_gnode(Grades *sent)
    and the function call would be
    Code:
    add_gnode(list->grade_sent)
    but this doesnt work..

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Write a function for finding a node by name? Give me your whole program, por favor.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    Working with unions, you have to access Fails or Pass in the function to correctly modify a grade. Keep in mind that unions only interpret the same memory block differently depending on what is accessed, so while student A may have passed, if you access the Fails field it will look like he failed.

    It would be better to simply strip the unions altogether and have separate members: a grade and a boolean for pass/fail status.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Or one could simply make two lists. Though the fields of this particular struct baffle me.... Like what exactly are you keeping track of?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I find it difficult to figure out how you know which of the two sets of structs you want to use:
    Code:
        union{
            struct{
                int num_fail;
                double grade;
            } Fails;
            struct{
                int num_pass;
                double grade;
            } Pass;
        } grade_specifics;
    I suspect you want to have a flag to say whether Fails or Pass is supposed to be used. Or are they in different lists? If so, I'm not sure you need to use a union.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 04:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21