Skip to main content
added 11 characters in body
Source Link
WhozCraig
  • 421
  • 4
  • 7
struct node *root = NULL, **pp = &root;
char buf[MAXBUF];buf[MAXBUF] = {0};
size_t size = 0;

while (fgets(buf, MAXBUF, f) != NULL)
{
    *pp = malloccalloc(1, sizeof(**pp));
    strncpy((*pp)->data, buf, MAXBUF-1);
    pp = &(*pp)->next;
    ++size;
}
*pp = NULL; // terminate the last node 
struct node *root = NULL, **pp = &root;
char buf[MAXBUF];
size_t size = 0;

while (fgets(buf, MAXBUF, f) != NULL)
{
    *pp = malloc(sizeof(**pp));
    strncpy((*pp)->data, buf, MAXBUF);
    pp = &(*pp)->next;
    ++size;
}
*pp = NULL; // terminate the last node 
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 
added 1059 characters in body
Source Link
WhozCraig
  • 421
  • 4
  • 7

The technique you're trying to accomplish in building your ordered linked list is called forward-chaining. And aA 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:

The only parts of this that requiremandate further elaboration arefollow this general algorithm:

  1. 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.
  2. Acquire a new node, storing its address in *pp, where pp is the pointer-to-pointer from (1)
  3. 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.
  4. 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 followingcode:

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 the root pointerroot. From there...

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. ItOn subsequent passes it will change asalways hold the iterations move throughaddress of the file, but`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 stringcurrent node is finished being configured copy, then this is done:

Thats it. Best of luck.

For C99 usersand later,

Hope it helps

The technique you're trying to accomplish in building your ordered linked list is called forward-chaining. And a pointer-to-pointer makes the task trivial. Error checking memory allocations not-withstanding, it is done something like this:

The only parts of this that require elaboration are the following:

The above line declares the root pointer, initialized to NULL, and a pointer-to-pointer that holds the address of the root pointer. From there...

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. It will change as the iterations move through the file, but it always holds the address of pointer that is to receive the next new node.

After the string copy, then this is done:

Thats it. Best of luck.

For C99 users,

Hope it helps

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:

The parts of this that mandate further elaboration follow this general algorithm:

  1. 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.
  2. Acquire a new node, storing its address in *pp, where pp is the pointer-to-pointer from (1)
  3. 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.
  4. 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:

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

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:

For C99 and later,

Post Migrated Here from stackoverflow.com (revisions)
Source Link
WhozCraig
  • 421
  • 4
  • 7

The technique you're trying to accomplish in building your ordered linked list is called forward-chaining. And a pointer-to-pointer makes the task trivial. Error checking memory allocations not-withstanding, it is done something like this:

struct node *root = NULL, **pp = &root;
char buf[MAXBUF];
size_t size = 0;

while (fgets(buf, MAXBUF, f) != NULL)
{
    *pp = malloc(sizeof(**pp));
    strncpy((*pp)->data, buf, MAXBUF);
    pp = &(*pp)->next;
    ++size;
}
*pp = NULL; // terminate the last node 

How It Works

The only parts of this that require elaboration are the following:

struct node *root = NULL, **pp = &root

The above line declares the root pointer, initialized to NULL, and a pointer-to-pointer that holds the address of the root pointer. 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. It will change as the iterations move through the file, but it always holds the address of pointer that is to receive the next new node.

After the string 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.

Thats it. Best of luck.


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 users,

for (struct node *ptr = root; ptr; ptr = ptr->next)
    printf("data: %s\n", ptr->data);

Hope it helps