Well, the "insert" in a linked list is just a "link it in", which is done in the else-part. In the initial link, there is nothing other than the head to "link in" - and the next link is NULL, which is already done above the if-else (since we ALWAYS insert at the end, next is always NULL on a new node). The else part sets the current tail's next pointer to the new node, then moves the tail to the new last element.
Insert element 3 in a 1, 2 list:
Code:
before:
head -> 1
|
v
tail -> 2
|
v
NULL
after tail->next = node:
head -> 1
|
v
tail -> 2
|
v
3
|
v
NULL
after tail = node:
head -> 1
|
v
2
|
v
tail -> 3
|
v
NULL
--
Mats