This code is supposed to append two linked lists, I totally fail to see how this code appends the two Cell Structs passed as arguments, as no manipulating is happening to the second parameter. it justs asks for the next node in the first Cell – so how can this work?
void Append(Cell *& first, Cell* second)
{
if (first == NULL)
{
first = second;
}
else {
Append(first->next, second);
}
}
The
elseblock of the function keeps following thenextpointer offirstuntil it gets to the end of that list. That is, untilfirst->nextisNULLand theelsedoes not get executed.Now the base case in the
ifblock. Whenfirst->nextisNULL, it changes that pointer to point atsecond, which is presumably the first element in another list.The reason there is some effect is because the
first->nextpointer is passed by reference. Modifying the pointer will modify the actual pointer at the end of the list, rather than just modifying a copy.