Chapter 5 · Simulation

Doubly Linked List Simulator

← Chapter 5
Ready.

Doubly Linked List Simulator

Every node of a doubly linked list has both a next and a prev pointer, and the list keeps head and tail pointers. You can walk it in either direction, and you can unlink a node without first hunting for its predecessor.

How to use it

  • With a Value: Add First, Add Last, Remove First, Remove Last (up to 7 nodes).
  • With an Index: Access, Add at Index, Remove at Index. Clear empties the list.
  • Linear Search, Traverse → (from the head), Traverse ← (from the tail) and Reverse are animated; Cancel stops them.
  • Highlighted arrows show which next / prev links change.

Costs

OperationTime
Add / remove at the head or tailO(1)
Access index iO(min(i, n − i)) from the nearer end
SearchO(n)
Reverse (swap prev/next in every node)O(n)

The price of the second pointer: more memory per node and more links to update on every insertion or removal.