Two Neighbor Links in a List

A doubly linked list records a previous and next reference for each node. An edit should preserve their agreement.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Walk a deletion
  2. Exercise both ends
  3. Trace both directions after removing one node
  4. Check head and tail ownership explicitly

Walk a deletion

For A, B, C, removing B when it is already located requires setting A's next to C and C's previous to A. Updating only one link makes backward traversal disagree with forward traversal. Traversing ordinary linked nodes explains the forward half.

Exercise both ends

Removing the head or tail requires updates to any stored head or tail references. Removing the sole element updates both. Write boundary tests for these cases because an interior deletion would never exercise either branch.[1]

Trace both directions after removing one node

To remove B from A, B, C, link A forward to C and C backward to A. Updating only the forward link leaves backward traversal reaching the removed node. Where the list owns B's links, clearing them can prevent stale traversal through a detached handle. See sentinel nodes.

Before: A.next=B, B.prev=A, B.next=C, C.prev=B
After:  A.next=C, C.prev=A
Forward A → C; reverse C → A

Check head and tail ownership explicitly

Removing the first real node can change the list's head. Removing its only element can change head and tail together. Test reverse traversal after removing an interior node, a head and a singleton. Include forward and backward link consistency assertions.

References

  1. Jeff Erickson, Algorithms ↩