taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 2.2.4-1

Exercise 2.2.4-1

not stated yet in Lean solution written

Rating21
KindK2, decide: say whether a claim holds from the written solution
SectionSection 2.2.4 of Volume 1

The task

Decide whether using PTR=LOC(PTR)\mathrm{PTR} = \mathrm{LOC}(\mathrm{PTR}) for the empty list, instead of PTR=Λ\mathrm{PTR} = \Lambda, 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 LINK(LOC(PTR))\mathrm{LINK}(\mathrm{LOC}(\mathrm{PTR})) is the contents of the variable PTR\mathrm{PTR} itself.

Solution

Show the solution

No. The convention does not remove any test, and in one place it makes things worse.

Let PTR\mathrm{PTR} point to the right end of a non-empty list, so that LINK(PTR)\mathrm{LINK}(\mathrm{PTR}) is the left end. For a new node PP the two insertions are LINK(P)←LINK(PTR)\mathrm{LINK}(P) \leftarrow \mathrm{LINK}(\mathrm{PTR}) and LINK(PTR)←P\mathrm{LINK}(\mathrm{PTR}) \leftarrow P, followed by PTR←P\mathrm{PTR} \leftarrow P in case (b). These lines are wrong for an empty list in the new convention. Then PTR=LOC(PTR)\mathrm{PTR} = \mathrm{LOC}(\mathrm{PTR}), so LINK(PTR)\mathrm{LINK}(\mathrm{PTR}) is the variable PTR\mathrm{PTR} itself. The first line sets LINK(P)\mathrm{LINK}(P) to LOC(PTR)\mathrm{LOC}(\mathrm{PTR}), and the second line stores PP into the variable. The node PP then points at the cell of the variable and not at itself, and the cell points back at PP. 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 PTR=LOC(PTR)\mathrm{PTR} = \mathrm{LOC}(\mathrm{PTR}) instead of PTR=Λ\mathrm{PTR} = \Lambda. The branch sets LINK(P)←P\mathrm{LINK}(P) \leftarrow P and PTR←P\mathrm{PTR} \leftarrow P, exactly as before.

For (c), the test for underflow is PTR=LOC(PTR)\mathrm{PTR} = \mathrm{LOC}(\mathrm{PTR}). The test for a list with one node, PTR=LINK(PTR)\mathrm{PTR} = \mathrm{LINK}(\mathrm{PTR}), is still needed, and its action is now PTR←LOC(PTR)\mathrm{PTR} \leftarrow \mathrm{LOC}(\mathrm{PTR}). 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 Λ\Lambda, which is a constant.

The convention would work without special cases only if the cell LOC(PTR)\mathrm{LOC}(\mathrm{PTR}) 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 PTR=P\mathrm{PTR} = P, LINK(P)=LOC(PTR)\mathrm{LINK}(P) = \mathrm{LOC}(\mathrm{PTR}). The next insertion at the left would put the new node between PP and the cell of the variable, and a traversal from LINK(PTR)\mathrm{LINK}(\mathrm{PTR}) would walk through the cell. So the uniform lines do not give a list of the items.

Remarks

The convention Λ\Lambda 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.