Linked Node Data Structure
Context: FIT1008_MOC Β· the non-contiguous alternative to an Array (Data Structure) Β· made of Nodes Β· realised as LinkList
Quick Revision
- π― Objective: store a collection as a chain of Nodes β no contiguity, no random access; reach a position by walking links.
- π¦ Core Components: singly / doubly / circular / sentinel variants.
- β‘ Key Constraint: insert/delete at a held node vs to reach index β the exact mirror of an array.
π Core
1. The Structure (Chain, No Random Access)
- Composition β chain of Nodes, each item + a link to the next β not contiguous, no random access.
- Fundamental trade β insert/delete at a held node (rewire links) vs to reach index β arrays are the mirror image.
2. The Linked-List Family
- Singly β head, tail/predecessor.
- Doubly β
prev+nextβ delete-given-node + tail (with tail pointer) β deque/LRU. - Circular β tailβhead β round-robin.
- Sentinel β dummy head/tail nodes β remove empty-list/head-insertion special cases (every real node has a predecessor).
βοΈ Core Decision Matrix
| Operation | Array (Data Structure) | Linked nodes |
|---|---|---|
| access index | ||
| insert/delete at a known node | (shift) | (relink) |
| insert/delete at index | walk + relink | |
| memory | one block (+ spare); Dynamic Array Resizing | per-node pointer; grows freely |
| cache locality | excellent | poor (pointer chasing) |
When It Flips: prefer linked for frequent front/held-node insert-delete with little indexed access (stacks, queues, LRU caches, adjacency lists); prefer array for random access, binary search, cache-sensitive scans.
π Exam Execution Trace
Manual Execution Trace
Insert at index decomposed:
| Step / State | Phase | Cost |
|---|---|---|
| 0 (Init) | β | β |
| 1 | walk head β node | |
| 2 | rewire new.link, prev.link | |
| total | insert-at-index |
β οΈ Common Mistakes
- π‘ β insertβ needs the node in hand β reaching index first is still , so insert-at-index is , not .
π§ Active Recall
State the array-vs-linked trade-off across access, structural edits, and cache behaviour.
- Hint: They optimise opposite operations.
Answer
- Short answer: Array = access, insert/delete, great locality; linked = access, held-node edit, poor locality.
- Why: Contiguity vs pointers β array needs resizing to grow; linked grows a node at a time at one pointer per element.
What do sentinel (dummy) nodes buy, and why are they used in production?
- Hint: Remove edge cases.
Answer
- Short answer: Every real node always has a predecessor/successor βΉ head insertion and empty-list handling stop being special cases.
- Why: Fewer null checks β eliminates a class of off-by-one/null-pointer bugs at the cost of one or two extra nodes.
Why can't binary search run on a linked list, and what's the consequence for a sorted one?
- Hint: No midpoint.
Answer
- Short answer: Reaching the middle is , so a sorted linked list stays for search.
- Why: Only splice survives β it keeps splice once positioned; for lookups use a SortedArrayList or balanced BST.