List (ADT)

Context: FIT1008_MOC · the most general linear Abstract Data Type (ADT) · backbone clustering ArrayList / LinkList / LinkListIterator · stacks & queues are it with access restricted to one end

Quick Revision

  • 🎯 Objective: ordered, any-position collection ➔ the general linear ADT; stacks/queues are one-end-restricted cases.
  • 📦 Core Components: Contract__getitem__/insert/delete_at_index | ArrayList access, shift | LinkList relink, walk | LinkListIterator mutate-in-traversal.
  • ⚡ Key Constraint: array vs linked are mirror images ➔ bottleneck = whichever of random access vs structural edit the workload hits.

📝 Core

1. List Contract (Ordered, Positional)

  • Positional access ➔ elements have positions ➔ touch any index via __getitem__/insert/delete_at_index.
  • Ordered ≠ sorted ➔ order is position, not value ([20,5,30,1] valid); no is_full (array resizes).
  • Operation reuseremove = index + delete_at_index | append = insert(len, …).

2. ArrayList (Array-Backed)

  • Storage substrate ➔ fixed Array (Data Structure) + length; head at 0, data 0..length-1.
  • Access vs edit ➔ random access | order-preserving insert/delete shift | grows via Dynamic Array Resizing.
  • remove is either way ➔ head ⟹ cheap index + full shift; tail ⟹ full scan + no shift.
  • Storage substrateNode chain + head; reach an index by walking links ().
  • Relink is ➔ the culprit is the walk __get_node_at_index, not the relink.
  • Insert ordernew→rest then prev→new or the tail leaks; tail pointer / doubly-link fix cases.

4. LinkListIterator (Walk & Mutate)

  • Cursorcurrent pointer, one item per __next__ (the Iterator for a LinkList).
  • Modifying version ➔ holds list + previous ➔ delete/insert during traversal in .
  • vs fail-fastprovides controlled mutation instead of forbidding it (ArrayList same edit = ).

⚙️ Core Implementation

🔹 ArrayList — access, shift

🔹 LinkListIterator — mutate while traversing

⚖️ Core Decision Matrix

OperationArrayListLinkListCause
__getitem__ / __setitem__direct index vs walk links
insert / delete at head (shift) (relink)move all vs repoint head
insert / delete at index walk + relinkshift vs locate-then-relink
append amortised tail-ptr / walkresize vs reach end
index (search)linear scan either way
memory / localitycontiguous, cache-friendly pointer/node, scattered

When It Flips: array = access / edit; linked = the exact inverse ➔ choose array for index-heavy/read-heavy, linked for frequent insert/delete at known positions. In-place delete = (LinkListIterator relink) vs (ArrayList shift).

📊 Exam Execution Trace

Manual Execution Trace

LinkList.insert(2, X) on head→A→B→C:

Step / StateTrigger OpPointer ActionList Payload
0 (Init)startwant index 2head→A→B→C
1locate prevprev = node@1prev = B
2new.link = prev.linkX→C (B still →C)head→A→B→C, X→C
3prev.link = newB→Xhead→A→B→X→C

Reverse steps 2–3 ⟹ B→X first overwrites prev.link (→C) before X captures it ⟹ C onward leaked.

Applied Exercise

Problem: Prove ArrayList.remove(item) is regardless of the item’s position. Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: remove = index + delete_at_index ⟹ one component is always , so the sum is .

🧠 Active Recall