14. Arrays and Linked Lists#
A resizable product list and a chain of linked nodes can represent the same sequence while doing very different work. This chapter develops both representations and compares access, growth, insertion, and removal under explicit invariants. You will test the sequence contract and justify a representation for a workload; Chapter 16 develops search and sorting algorithms separately.
Contiguous storage: Array-backed sequences keep elements next to each other for fast indexed access.
Dynamic array: A dynamic array grows by allocating larger storage and copying existing values.
Linked node: A linked structure stores values in nodes connected by references.
Insertion and deletion: Different representations make middle updates cheaper or more expensive.
Performance tradeoff: Choosing a sequence representation depends on access, growth, and update patterns.
Chapter Video
Data Structures Easy to Advanced Course - Full Tutorial from a Google Engineer (8:03:17) by freeCodeCamp.org.
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
This is a full course, not a required single viewing. Use its topic navigation to review this chapter’s concepts; the reading sequence below defines the chapter scope.
Learning Objectives
By the end of this chapter, you will be able to:
Explain how array-backed and linked representations store the same sequence contract.
Trace resizing, shifting, and the total copy cost of repeated array-backed appends.
Implement generic array-backed and singly linked sequences while preserving count, head, and tail invariants.
Separate the cost of locating a position from the cost of insertion or deletion at that position.
Test empty, singleton, duplicate, and invalid-index cases and justify a representation using workload and memory-locality evidence.
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Linear list |
A sequence of items arranged in order. |
2 |
Array representation |
A sequence representation that stores items in contiguous indexed positions. |
3 |
Linked representation |
A sequence representation where nodes refer to neighboring nodes. |
4 |
Dynamic array |
An array-backed list that resizes when it needs more capacity. |
5 |
Capacity |
The number of items an underlying array can currently hold. |
6 |
Resize |
Allocating a larger array and copying items into it. |
7 |
Node |
An object that stores one value and one or more links. |
8 |
Link |
A reference from one node to another. |
9 |
Head |
The first node in a linked list. |
10 |
Tail |
The last node in a linked list. |
11 |
Traversal |
Visiting list items from one position to the next. |
12 |
Insertion |
Adding a new item to a sequence. |
13 |
Deletion |
Removing an item from a sequence. |
14 |
Indexed access |
Retrieving an item by numeric position. |
15 |
Search cost |
The work needed to find a value in a sequence. |
16 |
Amortized cost |
Total work over a sequence of operations divided by its length. |
17 |
Invariant |
A representation rule preserved by every completed operation. |
18 |
Memory locality |
Accessing storage locations close together, often helping sequential access. |
19 |
Count |
The number of logical sequence elements currently stored. |
20 |
Location cost |
The work required to find an index or node before changing the sequence. |
21 |
Insertion gap |
A legal insertion position from zero through Count, including the gap after the final element. |