Storing data in a linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhen18
    New Member
    • Dec 2009
    • 9

    Storing data in a linked list

    I am doing a program which parses/separates a list of facebook friends' comma-separated data then stores them into a linked list.

    Lets say the list contains these: (format : First Name, Last Name, Birthdate, Gender, Status)

    John,Doe,1/1/1990,m,I love burgers!
    James,Dean,2/3/1867,m,I need some action!
    Marilyn,Monroe, 5/9/1825,f,I need some rest!

    Then I have this structure

    Code:
    typedef struct _node_t {
        Item iData;
        struct _node_t *pNext;
    } node_t
    where iData consists of

    Code:
    typedef struct {
        char *first_name;
        char *last_name;
        char *birthdate;
        char sex;
        char *status
    } Item;
    Then I have a working code for separating the data to first name, last name, etc
    but I'm having a problem with storing them into the linked list. I get a segmentation fault everytime.

    I'm supposed to have these ff functions:
    Code:
    node_t * createNode(Item iData) {
        /*
        Missing Code must:
        Allocate new node
        Set data member
        Set next member
         Return new node
        */
    }
    void insertNode(node_t * pHead, Item iData)
    {
           node_t *pNew;
           node_t *pCur;
    
           pCur = pHead;
           pNew = createNode(iData);
    
          /*
           Missing Code must:
           Insert the created node into a linked list
          */
    }
    such that in my main program

    Code:
    int main (void){
     
       node_t head;
       head.pNext = NULL; 
       //Other Variable Declarations
       .
       .
       .
       //PARSING CODE HERE
       //After parsing and storing the parsed data into the structure Item
       insertNode(&head, iData);
    }

    can you suggest some codes for the said functions??
    Thanks in advance!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Where do you get the segfault? Please post all of the relevant code.

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Segmentation faults come when you try to access memory which you are not supposed to access. Debug using gdb and pin point the exact location.

      Regards
      Dheeraj Joshi

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        ? I don't really get the responses so far but:
        Could you show us what you're doing in your missing code segments, eg what actual code are you trying where you have:
        /*
        Missing Code must:
        Allocate new node
        Set data member
        Set next member
        Return new node
        */

        There's more than one way to do it, so it'd be best if we helped you out in a way that matches your coding style.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          From what I remember of segmentation faults is that sometimes it's hard to pin point where it occurs and why.

          This can happen anywhere.
          It could be happening when you are inserting the node into the list.
          It could be happening when you are creating other nodes.
          It could be happening when you are setting any member in the node....

          If you are using development software that lets you step through your application you should start by doing that.

          If you aren't then you may want to start writing output at various stages in your code to narrow down where you think the segmentation fault is happening...

          Please remember that the segmentation fault may not actually be happening at the line where you stop at if you're print output...but it will give you a general idea...

          -Frinny

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            jkmyoung, I don't see what's hard to understand about them. I ask for all of the relevant code. Dheeraj suggests that the user debug his application with something like gdb. Simples.

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Must have had too many tabs open at once. I think that line was supposed to be for a different forum question; sorry.

              Comment

              Working...