Exercise · Volume 1 · 2.2.4-1
Exercise 2.2.4-1
not stated yet in Lean solution written
| Rating | 21 |
|---|---|
| Kind | K2, decide: say whether a claim holds from the written solution |
| Section | Section 2.2.4 of Volume 1 |
The task
Decide whether using for the empty list, instead of , makes the three basic operations on a circular list easier: (a) insert at the left end, (b) insert at the right end, (c) delete the left end.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Write each operation first for a list with at least one node, and then ask what the same lines do when the list is empty.
Hint 2
Remember that is the contents of the variable itself.
Solution
Show the solution
No. The convention does not remove any test, and in one place it makes things worse.
Let point to the right end of a non-empty list, so that is the left end. For a new node the two insertions are and , followed by in case (b). These lines are wrong for an empty list in the new convention. Then , so is the variable itself. The first line sets to , and the second line stores into the variable. The node then points at the cell of the variable and not at itself, and the cell points back at . The circle contains a cell that is not a node of the list.
So an insertion into an empty list still needs a separate branch, now guarded by instead of . The branch sets and , exactly as before.
For (c), the test for underflow is . The test for a list with one node, , is still needed, and its action is now . Without that test the deletion would leave a pointer to the freed node. Comparing with the address of a variable is also no cheaper than comparing with , which is a constant.
The convention would work without special cases only if the cell were made an honest node, a list head, which is the idea of the next section of the text. Then the list is no longer a pure circular list of the items.
Check
Take an empty list and run the uniform lines. The result is , . The next insertion at the left would put the new node between and the cell of the variable, and a traversal from would walk through the cell. So the uniform lines do not give a list of the items.
Remarks
The convention for the empty list costs one comparison in each operation and no extra cell. This is why the text keeps it.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.