6
I didn’t understand what head or (head) of the list means, and what is the advantage in using it?
6
I didn’t understand what head or (head) of the list means, and what is the advantage in using it?
6
It is the initial address of the list, it is the first element that is necessary to know where to start walking through it. This information needs to be in the data structure (the instance of the list). Then the location of the other elements will find in the elements themselves, since the linked list has as characteristic precisely have nodes with the value and the next element.
If the list is double chained it also needs a tail
to start backwards. It is common to have the tail
, even in list simply chained as optimization to facilitate insertion at the end of the list without having to go through it all.
In a certain way think that he is one of the nodes that has no value and that is already enclosed within the structure of the list. So much so that in many cases the head
is just a node and nothing else and it is confused with the list itself. Remembering that the end is a node that does not have a pointer to the next element.
It is not possible to make the attached list without a head. If not, it would start where? What you can do is the head be the list itself. If you don’t need additional information to control the linked list, and in simple cases you don’t even need it, then save a node in the variable that will contain your list, this node is the head. The head does not necessarily need to have a special treatment in simple cases, but there needs to be a place that starts the list.
Something like this works if you don’t 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;
I put in the Github for future reference.
In this case it’s just pointing to NULL
as well as being the first element is also the last. Once you insert a new element, the address of the new one will be placed in head->next
, replacing the NULL
, as it will in all other elements. This is the basic operation of a linked list.
Browser other questions tagged c chain-list
You are not signed in. Login or sign up in order to post.
http://www.cprogressivo.net/2013/10/Lista-simplesmente-encadeada-com-cabeca-em-C-Inserindo-nos-no-inicio-e-fim.html
– Mineiro
Possible duplicate of What is a chained list?
– Mineiro