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-8

Exercise 2.2.4-8

not stated yet in Lean solution written

Rating10
KindInformal: the exercise has no formal statement, so Lean does not state it from the written solution
SectionSection 2.2.4 of Volume 1

The task

Explain why Algorithm A keeps Q1Q_1 one step behind QQ.

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

Ask what a deletion or an insertion before NODE(Q)\mathrm{NODE}(Q) needs to know.

Solution

Show the solution

A node can be removed from a linked list only if we know the node that points to it, because that node's link must be changed. In Algorithm A, when two terms cancel, the node QQ is removed from the polynomial. When a term of PP is not in QQ, a new node must be put in front of QQ. Both actions change the link of the node before QQ, and the lists are one-way, so this node cannot be found from QQ without a new walk. The pointer Q1Q_1 holds it all the time.

There are ways without Q1Q_1. To delete NODE(Q)\mathrm{NODE}(Q) one can copy the next node into it and free the next node. To insert before NODE(Q)\mathrm{NODE}(Q) one can put the new node after it and exchange the contents of the two nodes. These tricks work, but they move nodes. In the polynomial lists the last node, the sentinel, would sometimes be moved, and then any other variable that points at the sentinel, for example the pointers PP and QQ that stand for the polynomials, would point to a wrong place. The extra pointer is the cheaper and safer choice.

Remarks

The extra pointer costs one register, and in Program A it also costs one instruction to advance it in each step.

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.