How to recognize the meaning of a linked list function in c++ [closed]

You can take a reasonable guess as to what these functions could do from just the declarations.

bool (List, int)

Takes a list and an element and returns a true/false answer. An immediate guess is “does this list contain this element”. We then look at the definition (*), see that the null list is false, the list starting with x is true, and otherwise we look at the tail for the answer. Thus our guess was correct.

void (List&, int)

Takes a list reference, and an element, and returns nothing. An immediate guess is that it adds an element to a list somewhere. Looking at it’s definition, we see that a new node is allocated, and initialised as an element and made to be the head of the modified list. Again, our guess helped identify exactly what it does.

List (List, List)

Takes two lists and returns a list. An immediate guess is that it joins the two lists together. And we see that it basically does this, but only adds values from the second list that are not present in the first.

(*) and fix the undefined behaviour of assigning nullptr to u rather than comparing it.

Browse More Popular Posts

Leave a Comment