Linear Data Structures
The simplest ways to organize data — rows, stacks, and queues.
You have learned to measure algorithm speed with Big-O. Now the question becomes: where do the data actually live? The answer — contiguous memory blocks, scattered pointer chains, or restricted-access stacks and queues — determines which operations are fast and which are slow.
These four linear structures are the foundation of every data-intensive program, from spreadsheets to neural networks. This article makes their trade-offs tangible.
Arrays — Contiguous Memory with Index Access
Definition
Array
Analogy
Example
Definition
Example: Python List Operations
lst = [10, 20, 30, 40, 50]
lst[3] # → 40 O(1) index access
lst.insert(2, 25) # → [10, 20, 25, 30, 40, 50] O(n)
lst.append(60) # → [..., 60] amortized O(1)
30 in lst # → True O(n) linear searchComplexity Table: Array
Operation Complexity
──────────────────────────────
Access (index) O(1)
Update (index) O(1)
Append amortized O(1)
Insert/Delete O(n)
Search O(n)Caution
Misconception: Python's list Is a Linked List
Python's list is internally a dynamic array with O(1) index access — the name is misleading. A true linked list has no index arithmetic and requires O(n) traversal to reach element k. To accommodate differently sized elements (like text and numbers) in the same list, Python internally stores only equal-sized pointers to the actual data.
When a dynamic array is full, it allocates a new, larger block (in the textbook case with double the capacity) and copies all elements. This resize costs O(n), but happens rarely enough that the amortized cost per append stays at O(1). Python's list uses the same idea, but only grows by about one eighth each time.
# Capacity doubling (simplified)
Capacity: 4 → full → new capacity: 8
→ full → new capacity: 16
# 1000 append operations:
# ~8 resizes, rest writes directly
# → amortized O(1) per append()Linked Lists — Scattered Nodes with Pointers
Definition
Linked List
Analogy
Example
Definition
Example: Insert at Position 500,000
An array with 1,000,000 elements: inserting at position 500,000 requires shifting ~500,000 elements → O(n). A linked list with a pointer to position 499,999: rewire 2 pointers → O(1). But: finding position 499,999 from the head costs O(n) traversal. The total cost is also O(n) unless you already have a reference to the position.
Variants: Singly vs. Doubly Linked
A singly linked list has pointers in one direction only (forward). A doubly linked list adds backward pointers, making deletion and backward navigation more efficient. Python's collections.deque uses a doubly linked structure for O(1) operations at both ends.
Complexity Table: Linked List
Operation Complexity
──────────────────────────────────────────
Access (position k) O(n)
Search O(n)
Insert/Delete at head O(1)
Insert at known position O(1)
Insert after search O(n)Caution
Misconception: Linked Lists Are Always Better
The O(1) insertion only applies when you already have a pointer to the insertion site. In most real scenarios, you must first search for the position (O(n)), and arrays have better cache locality — making them faster for many workloads despite expensive middle-insertions.
Linked lists emerged in the 1950s from early AI research. Newell, Shaw, and Simon developed IPL (Information Processing Language, around 1956), which used linked lists as a core structure. John McCarthy made linked lists the foundation of functional programming with LISP (1958).
Stacks and Queues — Access Disciplines
Definition
Stack & Queue
Analogy
Example
Definition
Example: The Call Stack
- 1. main() is called → Stack: [main]
- 2. processData() is called → Stack: [main, processData]
- 3. calculate() is called → Stack: [main, processData, calculate]
- 4. calculate() returns → Stack: [main, processData]
- 5. processData() returns → Stack: [main]
Function calls are managed as stack frames: each new call is pushed on top, and when a function returns, its frame is popped. This is exactly LIFO. If recursion goes too deep (e.g., fib(100000) without optimization), the stack overflows — a stack overflow.
Example: Web Server Queue
A web server receives 1,000 requests per second. Worker threads dequeue requests in arrival order and process them. FIFO ensures no request is starved — first come, first served.
Caution
Misconception: A Stack Is a Type of Array
A stack is a usage principle (LIFO contract) that can be implemented on an array, a linked list, or any other backing structure. The principle defines which operations are allowed (push/pop only), not how data is physically stored.
Each function call creates a stack frame with local variables and a return address. Python has a default recursion limit of ~1,000 frames. If exceeded, Python raises a RecursionError, which ends the program unless something catches it — Python's safeguard against the famous stack overflow.
import sys
print(sys.getrecursionlimit()) # → 1000
def infinite(n):
return infinite(n + 1)
infinite(0) # RecursionError: maximum recursion depth exceededFriedrich Bauer and Klaus Samelson formalized the stack in 1957 at what was then the Technische Hochschule München (today TU Munich) for evaluating arithmetic expressions. Agner Krarup Erlang founded queueing theory in 1909 for analyzing telephone networks — his work underpins modern network design.
Array vs. Linked List — The Comparison
Contiguous memory. O(1) index access, O(n) insert/delete. Good cache locality. Ideal when reading is more frequent than inserting.
Scattered nodes with pointers. O(n) access, O(1) insert at known position. No cache advantage. Ideal when frequent insertions/deletions at known positions dominate.
Complexity Comparison Across All Structures
Interactive: Complexity Matrix
Click on a data structure to see its strengths and weaknesses. Watch the color pattern: green means O(1) — the operation is lightning fast regardless of data size. Red means O(n) — runtime grows linearly with the number of elements.
Operations by Data Structure
Click a row for detail view
| Structure | Access (Index) | Search | Insert | Delete |
|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) |
| Linked List | O(n) | O(n) | O(1) | O(1) |
| Stack | O(n) | O(n) | O(1) | O(1) |
| Queue | O(n) | O(n) | O(1) | O(1) |
Note
Key Takeaways
- Arrays trade cheap reads (O(1) index) for expensive middle-insertions (O(n)) — choose them when you read far more often than you insert.
- Linked lists trade cheap local edits (O(1) pointer rewiring) for expensive traversal (O(n)) — choose them when insertions/deletions at known positions dominate.
- Stacks (LIFO) and queues (FIFO) are usage contracts, not storage types — they constrain how you access data, enabling powerful patterns like undo, recursion, and fair scheduling.
Check
Quiz: Linear Data Structures
What is the key difference between an array and a linked list?
Select one answer
1. What is the key difference between an array and a linked list?
2. You have a list of 1,000,000 entries and need to frequently insert elements in the middle. Which structure is more suitable?
3. A web server must handle incoming requests fairly — first come, first served. Which principle should it use?
4. Python's list is called 'list' but is internally a dynamic array. Why does this distinction matter?
Check
Learning Goals
Can you explain this in your own words? Tick what you've got.
0 of 3 done