Thread: Linked Lists.. that contain linked lists

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

    Linked Lists.. that contain linked lists

    What should I change in this code to implement a linked list... inside every node of a different linked list...

    Code:
    typedef struct _node{
        struct _Nodes_list *nlsent;
        struct _node *next;
        struct _node *prev;
    } NODE;
    
    typedef struct _Nodes_list{
        struct _Nodes_list *next;
        struct _Nodes_list *prev;
    } NODES_LIST;
    
    NODE list_add(NODE *sent)
    {
        NODE *new_node;
        new_node = (NODE *)malloc(sizeof(NODE));
        sent->prev->next = new_node;
        new_node->prev = sent->prev;
        new_node->next = sent;
        sent->prev = new_node;
    
        new_node->nlsent = (NODES_LIST *)malloc(sizeof(NODES_LIST));
        new_node->nlsent->next = new_node->nlsent;
        new_node->nlsent->prev = new_node->nlsent;
    }
    
    						
    NODES_LIST add_nodes_list_node(NODES_LIST *sent)
    {
        NODES_LIST *new_node;
        new_node = (NODES_LIST *)malloc(sizeof(NODES_LIST));
        sent->prev->next = new_node;  //<<<<Here I get a Seg Fault.
        new_node->prev = sent->prev;
        new_node->next = sent;
        sent->prev = new_node;
    											
    }
    if it helps, im getting a seg fault on the add_nodes_list_node()function on the 3rd line.. because I "havent initalized the sent->prev yet.. even though I thought i did that in list_add.. didnt I ???? What do I need to do ?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I'll tell you exactly what you need to do:

    Start drawing yourself pictures.

    If you aren't an experienced programmer, and you're attempting to pound this code out without some kind of visual reference, you're just asking for mistakes.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    if it helps, im getting a seg fault on the add_nodes_list_node()function on the 3rd line.. because I "havent initalized the sent->prev yet.. even though I thought i did that in list_add.. didnt I ???? What do I need to do ?
    If you post the code that calls them it would help.

    Please also read the FAQ entry about Casting malloc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I remember posting an example for you that basically would apply to this one as well.... Why not use that as a reference? It believe it compiled fine.

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, 05: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, 11: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