What is the purpose of the while (* variable) and if (! * variable) in the "while" and "if" statements?

7

In the Code Review there is a implementation of a simply linked list

asked by anonymous 08.11.2016 / 00:02

1 answer

4

In the code in question, List is set to

typedef node* List;

So, head is actually pointer pointer to a node, and *head , consequently saves a pointer to node. The while part could be written explicitly as

while (*head != NULL)
    head = &((*head)->next)

That is, it is in the list until it reaches the end of it (indicated by NULL). Similarly, the if part can be written as

if (*head == NULL) abort();

As it comes right after malloc, you are checking whether it returned NULL (this check was not done in the original code).

On the purpose, I believe that the advantage of implementing the code in question, through pointer pointers, is to avoid writing two versions of add () (one with empty list and one otherwise), as well as not being you need to have the last element point to the new one after inserting it.

    
08.11.2016 / 00:54