Thread: Trouble Iterating over Linked List

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    Trouble Iterating over Linked List

    I have a singly-linked list. I am confident that I have the list set up and the data populated. However, when I go to print out the contents of the list, I'm not able to traverse it.

    Code:
    int main(int argc, char *argv[]) {
        
    ...
    
        Neighbor *cursor, *root_ptr;
        Neighbor this_neighbor;
    
        // open the initialization file
        if ((file = fopen(argv[5], "r")) == NULL) {
            fprintf(stderr, "Failed to open file: %s\n", argv[5]);
    		time(&starttime);
            exit(-1);
        }
        
        root_ptr = malloc(sizeof(Neighbor));
        root_ptr->next = &this_neighbor; //keep a pointer to the root
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
    
            char* p;
            p = strtok(line, ",");
            p++;
    
            if (*p == *argv[1]) {
    
                p = strtok(NULL, ",");
                this_neighbor.send_to_using_port = atoi(p);
    
                p = strtok(NULL, ",");
                memcpy(this_neighbor.host, p, 1);
    
                p = strtok(NULL, ",");
                this_neighbor.neighbor_receives_on_port = atoi(p);
                
                p = strtok(NULL, ",");
                this_neighbor.link_cost = atoi(p);
    
                fprintf(stdout, "host: %s\tsend on: %d\t receives on: %d\tcost: %d\n", this_neighbor.host, this_neighbor.send_to_using_port, this_neighbor.neighbor_receives_on_port, this_neighbor.link_cost);
    
                Neighbor next_neighbor;
                this_neighbor.next = &next_neighbor;
                this_neighbor = next_neighbor;
                
            }
            
            this_neighbor.next = NULL;
        }
    
        // iterate over the lines in the file as a test
        // THIS IS THE PART THAT ISN'T WORKING
        cursor = malloc(sizeof(Neighbor));
        cursor = root_ptr;
        fprintf(stdout, "here\n");
        fprintf(stdout, "%s\n", root_ptr->host); //<-- BLANK LINE
        i = 1;
        while (cursor != NULL) {
            fprintf(stdout, "iteration number: %d\n", i);
            fprintf(stdout, "%s\n", cursor->host);        
            cursor = cursor->next;
            i++;
        }
        free (cursor);
    
    ...
        return 0;
    }
    My output looks like this:
    host: F send on: 6712 receives on: 7045 cost: 2
    host: G send on: 6713 receives on: 7046 cost: 2
    host: H send on: 6714 receives on: 7047 cost: 2
    Send Receive Link
    Neighbor Port on Port Cost
    -------- ----- -------- ----

    iteration number: 1

    iteration number: 2
    (blank line here)
    So you can see that wherever I am trying to output a value from my list (were I am trying to iterate over it), I am getting a blank line. Further, the loop only iterates twice where it should iterate 3 times.

    What am I doing wrong? Thanks!
    Last edited by joatmon; 11-03-2013 at 11:02 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should sit down with a piece of paper and graph out what your code for adding a new node is doing. Remember when you hit that brace on line 44 (in the posted code) you need to erase your next_neighbor node, as it will cease to exist at that point (every variable only exists inside the set of braces it is declared in). I suspect you'll find that you have "a big mess" when you are done.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Thanks. How then do I create a new node that will survive beyond the scope of the current iteration? I'm very confused.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Each new node on your linked list needs to be obtained by calling malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Thanks. I tried that method many times, but never successfully. I'm giving up. Linked lists hurt my brain. Arrays here I come.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with linked list
    By mikeman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2010, 11:10 PM
  2. Iterating through a linked list
    By Tom_Arch in forum C Programming
    Replies: 1
    Last Post: 04-24-2009, 08:24 PM
  3. Iterating Linked List
    By kentadams in forum C Programming
    Replies: 2
    Last Post: 07-02-2007, 09:26 AM
  4. Linked list trouble
    By sand_man in forum C Programming
    Replies: 2
    Last Post: 02-08-2005, 01:14 PM
  5. Having trouble with Linked List
    By hkmixxa in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2004, 03:25 AM