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_listfunction from Lists as collections so that it generates output in this format.Rewrite
print_listusing aforloop instead of awhileloop.The function
remove_secondpresented in Modifying linked lists will crash if passed a singleton or empty list. Fix it so that it returns aNULLin these cases instead.Add a modifier function to the templated
LinkListclass namedinsert_at_end(T value)that inserts a new node with cargo value,value, at the end of the list. Modify theLinkedListclass to give it a new instance variable,tail, that points to the last node in list. Changeinsert_at_endto make use oftail.Add a modifier function to the templated
LinkListclass namedinsert_at(T value, int pos)that inserts a new node with cargo value,value, at positionpos.posshould be the zero-based position of the new node after insertion, soposequal to 0 will insert in front, andposequal to 1 will insert the new node between the first and second (“zeroth” and “oneth”) nodes.Add a modifier function to the templated
LinkListclass namedinsert_in_orderthat 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
LinkListclass namedremove_from_endthat 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
LinkListclass namedremove_at(int pos)that removes the node at zero-based positionposand 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 withcargoequal tovalue.findshould return -1 ifvalueis not found in the list.Overload
findwith a second paramenter,startthat returns the number of the first node (starting withstart) in the list withcargoequal tovalue.find(T value, int start)should return -1 ifvalueis not found betweenstartand the end of the list.