What is head of the threaded list?

6

I do not understand what head or head ) of the list means, and what is the advantage in using it?

    
asked by anonymous 08.12.2016 / 13:43

1 answer

5

It is the initial address of the list, it is the first element that you need to know where to start walking through it. This information must be in the data structure (the list instance). Then the location of the other elements is finding in the elements themselves, since the linked list has as characteristic precisely to have knots with the value and the next element.

If the list is doubly chained you also need a tail to start the reverse. It is common to have tail , even in simply chained lists as optimization to facilitate insertion at the end of the list without having to go through it all.

In a way, think of it as one of the nodes that has no value and is already enclosed within the list structure. So much so that in many cases head is just a node and nothing else and it merges with the list itself. Remembering that the end is a node that does not have a pointer to the next element.

The list can not be linked without a head. If I had not, I'd start where? What you can do is head up the list itself. If you do not need additional information to control the linked list, and in simple cases you do not even need to, then save a node in the variable that will contain your list, this node is the head. The head need not necessarily have special treatment in simple cases, but there must be a place that starts the list.

Something like this works if you do not need a complex list:

typedef struct node {
    int value;
    struct node *next;
} Node;

Node *head = malloc(sizeof(Node));
if (head == NULL) {
    return 1;
}
head->value = 1;
head->next = NULL;

In this case it is only pointing to NULL because besides being the first element it is also the last one. As soon as you insert a new element, the new address will be placed in head->next , replacing NULL , just as it will in all other elements. This is the basic operation of a linked list.

More linked list information .

    
08.12.2016 / 13:48