The technique you're trying to accomplish in building your ordered linked list is called forward-chaining. A pointer-to-pointer makes the task trivial for building a simple linked list while retaining its input order. Error checking memory allocations not-withstanding, it is done something like this:
struct node *root = NULL, **pp = &root;
char buf[MAXBUF] = {0};
size_t size = 0;
while (fgets(buf, MAXBUF, f) != NULL)
{
*pp = calloc(1, sizeof(**pp));
strncpy((*pp)->data, buf, MAXBUF-1);
pp = &(*pp)->next;
++size;
}
*pp = NULL; // terminate the last node
How It Works
The parts of this that mandate further elaboration follow this general algorithm:
- Utilize a pointer-to-pointer to always hold the address of the pointer that will be populated with the address of the next node to add. The initial value of this pointer-to-pointer is the address of the
root
pointer: &root
.
- Acquire a new node, storing its address in
*pp
, where pp
is the pointer-to-pointer from (1)
- Once the node is finished being configured, change the address stored in
pp
to be the address of the next
member pointer held within the node just added. This step sets up the target location where the next node will be added if needed.
- When finished processing input, the last-node-added's
next
member pointer has its address currently held in pp
. If no nodes were added to the list, then pp
still holds the address of root
. To terminate the list, this pointer must be set to NULL.
So on to the code:
struct node *root = NULL, **pp = &root
The above line declares the root pointer, initialized to NULL (optional, but I hate indeterminate pointers anywhere in-code), and a pointer-to-pointer that holds the address of root
. From there...
*pp = malloc(sizeof(**pp));
This allocates a new node, storing its address at whatever pointer is currently being addressed by the pointer-to-pointer pp
. On the initial pass that pointer is the root
pointer. On subsequent passes it will always hold the address of the `next member within the last-node-added on a prior iteration. But it always holds the address of pointer that is to receive the next new node.
After the current node is finished being configured copy, then this is done:
pp = &(*pp)->next;
This stores the address of the just-added-node's next
member in the pointer-to-pointer. When we loop around for the next iteration, this is where we will hang the next new node. Finally, after the loop finishes, this is done:
*pp = NULL;
This sets the pointer pointed-to by pp
to NULL. Now think about what pointer that is:
- If no items were populated in the list, then
pp
still holds the address of root
and this will reestablish root
as NULL; exactly what you want if the list is empty.
- If any nodes were read, then
pp
will hold the address of the last added-node's next
pointer, which should set to NULL to terminate the linked list. Again, exactly what this code does.
Addendum: Printing the list
Per request, printing the list after this is simply:
struct node *ptr = root;
while (ptr)
{
printf("data: %s\n", ptr->data);
ptr = ptr->next;
}
For C99 and later,
for (struct node *ptr = root; ptr; ptr = ptr->next)
printf("data: %s\n", ptr->data);
root
as a dangling pointer, iffgets
doesn't give any lines? \$\endgroup\$strncpy
. You've already prevented overflowing the array by passing its size tofgets
. Further, whilestrncpy
won't overflow the array, it will corrupt your string if the input is too long, and future code will not work right. Read carefully about what it does. Then file it away as one of the functions that you're not going to use. \$\endgroup\$