Chapter 15 Exercise Set 0: Chapter Review¶
By convention, lists are printed with parentheses with commas between the elements, as in (1, 2, 3). Modify the
print_list
function from Lists as collections so that it generates output in this format.Rewrite
print_list
using afor
loop instead of awhile
loop.The function
remove_second
presented in Modifying linked lists will crash if passed a singleton or empty list. Fix it so that it returns aNULL
in these cases instead.Add a modifier function to the templated
LinkList
class namedinsert_at_end(T value)
that inserts a new node with cargo value,value
, at the end of the list. Modify theLinkedList
class to give it a new instance variable,tail
, that points to the last node in list. Changeinsert_at_end
to make use oftail
.Add a modifier function to the templated
LinkList
class namedinsert_at(T value, int pos)
that inserts a new node with cargo value,value
, at positionpos
.pos
should be the zero-based position of the new node after insertion, sopos
equal to 0 will insert in front, andpos
equal to 1 will insert the new node between the first and second (“zeroth” and “oneth”) nodes.Add a modifier function to the templated
LinkList
class namedinsert_in_order
that inserts a new node in a list of ordered nodes in the correct order, between the cargo value that proceeds it and the cargo value that succeeds it.Add a modifier function to the templated
LinkList
class namedremove_from_end
that removes the node at end of the list and returns its cargo value. Be sure not to leak memory as you do this!Add a modifier function to the templated
LinkList
class namedremove_at(int pos)
that removes the node at zero-based positionpos
and returns its cargo value. Again be careful not to leak memory.Add a member function named
find(T value)
that returns the number of the first node (starting with 0) in the list withcargo
equal tovalue
.find
should return -1 ifvalue
is not found in the list.Overload
find
with a second paramenter,start
that returns the number of the first node (starting withstart
) in the list withcargo
equal tovalue
.find(T value, int start)
should return -1 ifvalue
is not found betweenstart
and the end of the list.