Skip to main content
Contents Index
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Exercises 28.13 Exercises
1.
Write the
splice function for a
Linked List (with size and tail). The other list should end up completely empty.
You may assume both lists have at least one node.
2.
Write the
sliceInHalf function for a
Linked List (with size and tail). It should split the list into two halves. The first half should remain in this list, and the second half should be returned as a new list. If there are an odd number of nodes, the extra node should go in the second half.
You may assume the list has at least two nodes.
Hint 1 .
You will need to walk a pointer to just before the midpoint of the list. That should be (size / 2) - 1 steps from the start of the list.
Hint 2 .
Both lists wonβt necessarily end up with size = (size / 2). That is how long the first list should end up. Use the length of it to calculate the length of the second list.
For example, after splitting a list of 5 things, the original list should have (5 / 2) = 2 things and the new second list should have 3 things.
3.
Write the recursive
reverseToString function for the
Simple Linked List used in this chapter.
For a list containing nodes with values 10, 20, and 30, the function should return the string "30 20 10".
Loops have been intentionally disabled.
4.
You do not need to worry about what to do if the list is empty.
Loops have been intentionally disabled.
You have attempted
of
activities on this page.