Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Linked List Problem in C

  1. Nov 7, 2009 #1
    Hey guys,

    I'm trying to implement a linked list in C, with each node carrying fields from a text file. The problem is that whatever was the last line read from the text file gets placed into every single node. Even more strange is that when I add an additional node outside of the while loop it doesn't replace all the previous nodes. Here's the loop that's causing me problems. Any help would be much appreciated!

    Code (Text):
    while (!feof(file_ptrDR)) {
           
            newNode = (lnode *)malloc(sizeof(lnode));
                    newNode = NULL;
       
            c = fgetc(file_ptrDR);
            while (c != ',') {
                NAME[i] = c;
                i++;
                c = fgetc(file_ptrDR);
               
            }
           
            i = 0;
            c = fgetc(file_ptrDR);
            newNode->name = NAME;
            while ((c = fgetc(file_ptrDR)) != ',') {
                PASSWORD[i] = c;
                i++;
            }
            i = 0;
            c = fgetc(file_ptrDR); //get past the space
            (newNode->password) = PASSWORD;
            c = fgetc(file_ptrDR);
            while (c != '\n' && c != EOF)  {
                TYPE[i] = c;
                c = fgetc(file_ptrDR);
                i++;
            }
            i = 0;
            newNode->type = TYPE;
            newNode->next = tempNode;
            tempNode = newNode;
    }
    linkedlist = tempNode;
           
     
  2. jcsd
  3. Nov 7, 2009 #2

    rcgldr

    User Avatar
    Homework Helper

    Code (Text):

        newNode = (lnode *)malloc(sizeof(lnode));
        newNode = NULL;
     
    Why are you zeroing out newNode just after allocating it? Perhaps you wanted this?
    Code (Text):

        newNode = (lnode *)malloc(sizeof(lnode));
        newNode->next = NULL;
     
    Also you need to save the first instance of newnode in another variable so you have a pointer to the start of the linked list.
    Code (Text):

    static lnode * firstNode = NULL;
    ...
        newNode = (lnode *)malloc(sizeof(lnode));
        if(firstNode == NULL)
            firstNode = newNode;
     
     
    Last edited: Nov 7, 2009
  4. Nov 7, 2009 #3
    Making it null was just one of the various solutions I tried to rectify the problem. It didn't change the output when traveling through the list, but you're right, I should probably remove it.

    As for the firstNode, where would I place it? The way I designed it, I'm adding new nodes to the head, so why would I need to save the very first newNode?
     
  5. Nov 8, 2009 #4

    rcgldr

    User Avatar
    Homework Helper

    I didn't realize you wanted to create a LIFO (last in first out) linked list. I don't see how setting newNode = NULL isn't causing problems, because it will always throw away the pointer from malloc and instead set it to zero. I'm assuming that tempNode is initialized to NULL? Otherwise I don't see the problem. You might want to move the last i=0 to before the first part of the loop so it's clearly initialized to zero before the first time you parse "NAME".

    Have you tried stepping through this code for a few loops with a debugger to see what's going on?
     
  6. Nov 8, 2009 #5
    Making it not null didn't help.

    It seems that any nodes made in the while loop get turned into what the last node was before the while loop exits, but if I manually add another node after that, it doesn't affect the previous nodes. Frankly I'm at a loss after hours of staring at the code.
     
  7. Nov 8, 2009 #6

    rcgldr

    User Avatar
    Homework Helper

    So you did remove
    Code (Text):

         newNode = NULL;
     
    If you run with the debugger, did you check to make sure newNode is being assigned a different address each time call malloc?
     
  8. Nov 9, 2009 #7
    > Have you tried stepping through this code for a few loops with a debugger to see what's going on?

    Why don't they teach new programmers about the debugger? It is so much easier to spot mistakes and it also teaches users a lot. It is the number one tool to use and no one seems to know about it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Linked List Problem in C
Loading...