Exercise · Volume 1 · 2.2.5-2
Exercise 2.2.5-2
not stated yet in Lean solution written
| Rating | 22 |
|---|---|
| Kind | Informal: the exercise has no formal statement, so Lean does not state it from the written solution |
| Section | Section 2.2.5 of Volume 1 |
The task
Explain why a singly linked list cannot work efficiently as a general deque, while deletion at one end can be efficient.
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
Think about what a deletion at an end needs to know afterwards: where the new end is.
Solution
Show the solution
In a singly linked list every node knows only its successor, in one fixed direction. Say the links point from the left end to the right end. Deleting at the left end is easy: the new left end is the successor of the old one, and it is found by one link. Deleting at the right end makes the predecessor of the old right end the new right end, and no link leads to it. The only way to find it is to walk from the left end, and this takes time proportional to the length of the list.
If the links point the other way, the right end is cheap and the left end is not. A list that is used for repeated deletions at one end must therefore have its links pointing away from that end, since after each deletion we must know what to delete next. A deque deletes at both ends, so the links would have to go both ways, which is what a doubly linked list does.
Exercise 2.2.4-18 shows that the two links can be packed into one field by the exclusive or of the two neighbours. This gives general deque operations with one link field per node, but a node can be reached only when a neighbour is also known.
Remarks
Insertion at both ends of a singly linked list is not a problem if a pointer to each end is kept. It is the deletion at the far end that fails.
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.