tenkii
tenkii

Reputation: 449

C - Linked Lists

I am trying to understand the code of linked lists. I understand how they work. I am looking at some code to do with dynamic memory and linked lists, I have simplified it here:

 #include <stdio.h>
 #include <stdlib.h>

 typedef struct node {
     char *word;
     struct node *next;
 } node;

 void display_word(node *start) {
     node *start_node = start;
     puts("");
     for(; start_node != NULL; start_node = start_node->next) {
         printf("%s", start_node->word);
     }
 }

 node* create_node(char *input) {
     node *n = malloc(sizeof(node));;
     n->word = strdup(input);
     n->next = NULL;
     return n;
 }

 int main() {
     node *start_node = NULL;
     node *n = NULL;
     node *next_node = NULL;
     char word_holder[20];
     for(; fgets(word_holder,80,stdin) != NULL; n = next_node) {
         next_node = create_node(word_holder);
         if(start_node == NULL)
            start_node = next_node;
    if(n != NULL)
        n->next = next_node;
    }
    display_word(start);
 }

So the program creates a linked list of each word the user enters and then it prints it out. What I dont understand is in the main() function where next_node is assigned to a new node everytime to create a new one, but start_node points to next_node, so it will point to every new node that next_node creates each time? So how is it possible to still keep the list? Shouldn't we lose the old node each time?

Can someone explain please.

Upvotes: 0

Views: 214

Answers (3)

Eric Telmer
Eric Telmer

Reputation: 65

When the first node is created, a pointer to it is saved in start.

After every iteration of the loop, "n" is set to the node just created, because the last piece of the for loop (;n = next) is executed after every iteration of the loop. So mid loop execution "n" will always be pointing to the previous node. Therefore the statement n->next = next is setting the previous node's "next" pointer to the new node.

So during the second iteration of the loop, n = start, and start->next is set to "next" the node you just created.

Upvotes: 2

Ricky Mutschlechner
Ricky Mutschlechner

Reputation: 4409

I hope this answers your question - every time you are updating "next", you're setting that to be yet another new node. Each node has their own "next" that leads to the next node, so you aren't going to lose anything by doing it this way. I didn't actually test your code but since "Start" points to the first node always, you aren't going to lose any nodes along the way. A debugger should help if you're curious to learn more about how this works!

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 225242

  1. When the first node is created, a pointer to it is saved in start.

  2. When subsequent nodes are created, they are added at the end of the list, so start still points to the first node, and through it, the rest of the list.

Step through the code with a debugger, or get out a pencil and paper and draw what's happening as you step through in your brain, and you'll see how it all gets put together.

Upvotes: 2

Related Questions