1. ## How to use linked lists / sentinel

Hello, so I am trying to understand what a sentinel is. I got this assignment (under the LIST section) Lists and Vectors

I do not understand how sentinels work so I am unable to complete the functions... If someone could help me out, thanks

2. The way I read it, a sentinel is a list element that only exists to link to other elements. That way, you avoid NULL root and double pointers.

3. I never heard of sentinel being used to name this. I have heard of an empty or fake first node being used by old C link lists.

Tim S.

4. It's just
Code:
```void list_init(struct list *list) {
list->next = NULL;
list->data = 0;  // it really doesn't matter!
}

...

int main ( ) {
struct list mylist;
list_init(&mylist);
}```

5. I tried to write the second function
Code:
```int list_is_empty(struct list *list)
{
if (list -> next == NULL)
{
return 1;
}

return 0;
}```
However I don't understand for the following functions how I am supposed to write them. How to return the length of the list, excluding the sentinel ? it is not clear to me how I can treat this Thank you for you answers

Hmm I think I have understood that *list = &sentinel (at the bottom of the LIST paragraph), Is this correct ? Therefore, in each function when we have *list as argument, it is the sentinel's address. Is this correct ?

6. Just use a loop to count until you reach a null pointer.

7. I tried to write the third function
Code:
```    size_t list_len(struct list *list){
size_t i = 0;
while (list -> next != NULL)
{
i += 1;
}
return i;
}```
Does this look correct ?

8. No, as you do not change list->next, so if it is the case that list->next != NULL, then you will have an infinite loop.

9. Oh yeah right. I need to get list -> next to increment
Could I do list = list -> next ?

10. Does it make sense to you?

11. list->next = list-> next ?? I need to increment the pointer but how ?

12. I didn't say if you're right or wrong. I asked if that made sense to you

Put it another way: what does list = list->next mean?

13. for me list = list -> next is wrong because "list" has 2 attributes: data and next so we are not changing anything
how could we make the pointer move on to the next item ? Is it possible ?

14. Originally Posted by centrecmercial
for me list = list -> next is wrong because "list" has 2 attributes: data and next so we are not changing anything
Ah ok. We have come to the crux of the matter. I didn't want to tell you yes or no because you need to understand what you're doing, not merely guess at what might work until by chance you get it right and someone tells you that it is correct, even though you have no idea why it is correct.

list is a pointer. It either points to an object of type struct list, or it could be a null pointer of type struct list. If it points to an object of type struct list, that object could either be the sentinel, or it could be an actual element of the linked list. Despite the name, a struct list object that is an actual element of the linked list is a node in the linked list, not the linked list itself.

Now, a struct list object has both a pointer to the next struct list object, and the member named data that stores the data of the node. So if you want to traverse the linked list, you need to keep accessing the member named next of the current element so as to get the pointer to the next element. That is what list->next is doing. But then because you need to store it somewhere, and you no longer need list since you have already counted it, you can write list = list->next.

15. hmmmmmmmmmm I see
So basically list -> next is just accessing the next object in the list. It can be NULL or be existing and have a data attached to it
So list = list -> next is just storing the pointer of the NEXT element in the list !
And I can put this in the while loop ? yay

If this is correct let me try the next function (I like the way you explain because you don't give me the result I have to understand it )