The image below is my instructor's code on how to append a node to a linked list. I understand that the tail pointer is used to traverse through the list until it reaches a node with link containing NULL
. However, I do not understand how tail = tail->next
traverses through the list?
Asked
Active
Viewed 59 times
-2

CoffeeTableEspresso
- 2,614
- 1
- 12
- 30

papij
- 1
- 2
-
2probably duplicate of https://stackoverflow.com/questions/2575048/arrow-operator-usage-in-c – CoffeeTableEspresso Oct 09 '22 at 23:04
-
2"`->` operator in C" has been covered many times on stackoverflow – CoffeeTableEspresso Oct 09 '22 at 23:04
-
4If you have an instructor, why don't you ask them? Their job is to teach the language. – Barmar Oct 09 '22 at 23:05
-
2`tail` is a pointer to the current node. `tail->next` is the value of the `next` member in the node that `tail` points to. So if you reassign to that value, it traverses to the next node in the list. Do it repeatedly traverses until you reach the end. – Barmar Oct 09 '22 at 23:06
-
1`a->b` is equivalent to `(*a).b`. I.e. `a` is a pointer to a `struct` or `union`, and `b` is the member you wish to access. It's an example of what's called "syntactic sugar". Similarly, `a[b]` is equivalent to `*(a+b)`. – Tom Karzes Oct 09 '22 at 23:09
-
2Does this answer your question? [Arrow operator (->) usage in C](https://stackoverflow.com/questions/2575048/arrow-operator-usage-in-c) – Allan Wind Oct 10 '22 at 03:18
-
@Barmar but how does reassigning by tail = tail->next traverse to the next node. From my understanding, that line would just make tail be equal to whatever it's pointing to so it would be an endless loop. – papij Oct 10 '22 at 20:16
-
`tail` is first pointing to a node. Then it gets the `next` pointer from that node, and assigns that to `tail`. So now it's pointing to the node after it. – Barmar Oct 10 '22 at 20:20
-
Why do you think it makes it equal to whatever it's pointing to? That would be `tail = tail`. – Barmar Oct 10 '22 at 20:20
-
Put `printf("tail = %p tail.next = %p\n", tail, tail.next);` before and after the assignment, and you'll see what it's doing. – Barmar Oct 10 '22 at 20:21
-
It's similar to the way `n = n + 1` is how you would traverse through sequential numbers. – Barmar Oct 10 '22 at 20:22
-
@Barmar in the struct node definition o : struct node { int value; struct node* next; } or the main program, there is no line that suggests next is pointing to the link (next) on another node, so it seems as if tail = (tail*).next just keeps assigning to itself. Sorry if that does not make sense I'm just really confused about this. – papij Oct 10 '22 at 20:27
-
`next` points to the next node, not the tail of the next node. But after you do `tail = tail->next`, now `tail` points to the next node. And you repeat this each time. – Barmar Oct 10 '22 at 20:33
-
@Barmar how do we know that next is pointing to another node? – papij Oct 10 '22 at 20:35
-
Because its type is `struct node*`. So it can't point to anything other than another node. And the code that adds nodes to the list uses this to create the list. – Barmar Oct 10 '22 at 20:41
1 Answers
0
Let's look at this in memory. Suppose you have 3 nodes, at addresses 10
, 20
, 30
, and head
points to the node at address 10
. You'll have the following memory contents:
10: value = 1
next = 20
20: value = 2
next = 30
30: value = 3
next = NULL
head = 10
After you do tail = head
, we also have:
tail = 10
Now we do tail = tail->next;
. tail->next
dereferences tail
to get the structure at address 10
, then fetches the value from its next
member, which is 20
. After the assignment, we now have:
tail = 20
So this has stepped tail
to the next node in the list.
The next time through the loop, we do it again. tail->next
fetches the next
member in the structure at location 20
, which is 30
, so we'll now have:
tail = 30
The loop now stops, because with this value of tail
, tail->next
contains NULL
, so the condition while (tail->next != NULL)
fails.

Barmar
- 741,623
- 53
- 500
- 612