Thread: Link List help please

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Link List help please

    hi, i got a problem with my link list...at the last while loop, i realise dat currNode->next is null pointer. can anyone help me with this? thanks
    Last edited by pothios; 03-15-2008 at 12:14 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe the first of these:
    Code:
    		currNode->next = NULL;
    should actually be pointing to another node?

    --
    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.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    currNode->next = NULL;

    i thought i should set the next node to null since its the last node?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But you have [unless my eyes are playing tricks] two of those lines, the first one, I think should be something else.

    There are also two other problems:
    1. You are storing the same address for every single word, so your linked list will have the last word every time.
    2. You are allocating an extra node at the end of the list which isn't being used.

    --
    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.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    how do i solve the adress problem?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You've presented a code fragment, which makes things a bit more difficult, but this is close (and untested):

    Code:
    while(fgets(entry, sizeof(entry) , myfile)!=0)
    {
        // Create a new node
        currNode = malloc(sizeof(*currNode));
    
        // Allocate enough memory to hold the string you've just read
        currNode->words = malloc(strlen(entry) + sizeof(*entry));
        if (!currNode->words)
        {
           // Bail out of the prog altogether as it's likely all gone to hell
           exit(EXIT_FAILURE);
        }
    
        // Copy the string read in to the newly-allocated memory
        strcpy(currNode->words, entry);
    
        currNode->next = NULL;
        tailNode = currNode;
    }
    Last edited by rags_to_riches; 03-14-2008 at 08:17 PM. Reason: Missed a /

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You may have glanced at my code, but you didn't read it.
    The first time you hit currNode in the while loop...what is it pointing to?

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    nvm got it
    Last edited by pothios; 03-15-2008 at 12:39 AM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please don't edit your original post to remove most of it.
    That makes what other people have posted look like nonsense. It also stops others from being able to learn from your mistakes.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM