Linear Data Structures
Sequential Organization of Elements
Read aloud is unavailable in this browser.
How Elements Follow One Another
We've established that linear data structures organize elements in a sequence with clear predecessor-successor relationships. But how is this sequence actually achieved? How does an element "know" which element comes next?
This question leads us to one of the most fundamental concepts in data structures: the distinction between logical organization and physical organization. The logical sequence—the order we conceptually think about—must be realized through some physical mechanism in computer memory.
Understanding this mechanism is not merely academic. The way elements are physically organized determines the performance characteristics of every operation: access, search, insertion, deletion, and traversal. Poor understanding of sequential organization leads to poor algorithm choices and, ultimately, to slow software.
By the end of this page, you will understand the two fundamental approaches to sequential organization—contiguous storage and linked storage—along with their tradeoffs. You'll see how this single design decision shapes the entire behavior of a data structure.
Logical Order vs. Physical Order
Before diving into specific mechanisms, let's clarify a crucial distinction:
Logical Order
The conceptual sequence of elements as we intend them to be. If we have elements A, B, C, D in that logical order, then A is "first," B is "second," and so on. Logical order exists in our design—it's what we want the data structure to represent.
Physical Order
The actual arrangement of elements in computer memory. Memory is a vast array of bytes, each with a numeric address. How do we map our logical sequence onto physical memory locations?
The key insight is that logical order and physical order don't have to match. We can achieve a logical sequence where A precedes B precedes C even if:
- A, B, and C are in consecutive memory locations (physical order matches logical order), OR
- A, B, and C are scattered across memory, with explicit links connecting them
Both approaches create valid linear data structures, but with very different performance characteristics.
Computer memory (RAM) is fundamentally a linear array of bytes. Each byte has a unique address, and addresses are sequential integers. To store any data structure, we must map our abstract concepts onto these concrete memory locations.
| Aspect | Logical Order | Physical Order |
|---|---|---|
| Definition | Conceptual sequence intended by design | Actual byte arrangement in memory |
| Determined by | Application requirements | Implementation strategy |
| Example | Elements A, B, C in that sequence | Addresses 0x1000, 0x1004, 0x1008 |
| Flexibility | Fixed by problem domain | Can be designed for efficiency |
| Visibility | Seen by the programmer using the structure | Usually hidden by abstraction |
Contiguous Memory Organization (The Array Model)
The most straightforward way to achieve sequential organization is to place elements side by side in memory—in consecutive memory locations with no gaps. This is called contiguous storage or contiguous allocation.
How it works:
- Allocate a block of memory large enough to hold all elements
- Place the first element at the beginning of the block
- Place the second element immediately after the first
- Continue until all elements are stored
The physical order matches the logical order exactly. The successor of an element is simply the next memory location.
Address arithmetic:
If each element occupies S bytes, and the first element is at address A, then:
- Element 0 is at address
A - Element 1 is at address
A + S - Element 2 is at address
A + 2S - Element
iis at addressA + i × S
This formula enables the defining feature of arrays: O(1) random access. To access any element, we simply calculate its address with a multiplication and addition—operations that execute in constant time regardless of how many elements exist.
- •O(1) random access — Access any element by index instantly via address calculation
- •Cache efficiency — Adjacent elements are loaded together into CPU cache
- •Memory efficiency — No overhead for storing links or pointers
- •Simplicity — Traversal is just incrementing an address or index
- •Predictable iteration — Hardware prefetching optimizes sequential access
- •Fixed size (static arrays) — Size must be known at allocation time
- •Expensive insertion/deletion — Inserting in the middle requires shifting all subsequent elements: O(n)
- •Memory fragmentation — Large contiguous blocks may be unavailable
- •Resizing overhead — Growing requires allocating new memory and copying all elements
- •Waste potential — Pre-allocated space may go unused
Linked Memory Organization (The Linked List Model)
An alternative to contiguous storage is linked storage, where elements can be scattered across memory and are connected by explicit links (pointers or references).
How it works:
- Each element is wrapped in a node that contains:
- The element's data
- A link (pointer/reference) to the next node in the sequence
- Nodes can be allocated anywhere in memory
- The sequence is established by following the chain of links
The logical order is independent of physical order. Element B can be at memory address 5000, element A at address 7200, and element C at address 1300—as long as A's link points to B and B's link points to C, the logical sequence A → B → C is preserved.
Traversal mechanism:
To find element i, we must:
- Start at the first node (the "head")
- Follow links
itimes - Read the data from the node we land on
This takes O(i) time—no formula can jump directly to the middle. This is the fundamental tradeoff: we lose O(1) random access but gain flexibility.
- •Dynamic size — Grows and shrinks easily without pre-allocation
- •Efficient insertion/deletion — Once at the position, adding/removing takes O(1)
- •No memory waste — Each node is allocated only when needed
- •No fragmentation issues — Nodes can fit in any available memory
- •Flexible reordering — Changing order only requires updating links
- •O(n) random access — Must traverse from head to reach element i
- •Memory overhead — Each node stores extra pointer(s) in addition to data
- •Cache inefficiency — Non-contiguous nodes cause cache misses
- •Pointer complexity — Incorrect pointer manipulation causes bugs and crashes
- •Overhead per element — Small elements may be dwarfed by pointer storage
The Fundamental Tradeoff: Access vs. Modification
The choice between contiguous and linked organization is not about which is "better"—it's about understanding the tradeoff between access speed and modification flexibility.
The core insight:
- Contiguous structures excel at access — Computing an address is O(1), so reading/writing by index is instant
- Linked structures excel at structural modification — Inserting or removing a node is O(1) once you're at the position
The opposite is also true:
- Contiguous structures struggle with modification — Inserting requires shifting, which is O(n)
- Linked structures struggle with access — Reaching position i requires i steps, which is O(n)
This tradeoff shapes all decisions about linear data structure selection. When you understand it, you can predict performance characteristics without memorizing tables.
| Operation | Contiguous (Array) | Linked (List) |
|---|---|---|
| Access by index | O(1) — address calculation | O(n) — traverse from head |
| Access first/last | O(1) — known positions | O(1) with head/tail pointers |
| Search (unsorted) | O(n) — check each element | O(n) — check each element |
| Insert at beginning | O(n) — shift all elements | O(1) — update head pointer |
| Insert at end | O(1) amortized* — if space | O(1) with tail pointer |
| Insert in middle | O(n) — shift elements after | O(n) to find + O(1) to insert |
| Delete from beginning | O(n) — shift all elements | O(1) — update head pointer |
| Delete from end | O(1) — decrement size | O(n) singly / O(1) doubly |
| Delete from middle | O(n) — shift elements after | O(n) to find + O(1) to delete |
Ask yourself: What will I do more often—access elements by position, or insert/delete elements? If access dominates, favor contiguous. If modification dominates (especially at the ends), favor linked. Many real applications are read-heavy, which is why arrays are so common.
Understanding Memory Layout
To truly understand sequential organization, we need to visualize how elements actually sit in memory. Let's examine both models at the byte level.
Contiguous Layout Example:
Suppose we have an array of 4 integers, each 4 bytes, starting at address 1000:
Address: 1000 1004 1008 1012
+--------+--------+--------+--------+
Content: | 10 | 20 | 30 | 40 |
+--------+--------+--------+--------+
Index: [0] [1] [2] [3]
To access index 2: Address = 1000 + (2 × 4) = 1008. Done in one calculation.
Linked Layout Example:
Now suppose we have a linked list of 4 integers, each node containing a 4-byte integer and an 8-byte pointer:
Address 1000 (Node for 10): Address 2048 (Node for 20):
+--------+--------+ +--------+--------+
| 10 | → 2048 | | 20 | → 3072 |
+--------+--------+ +--------+--------+
Address 3072 (Node for 30): Address 4096 (Node for 40):
+--------+--------+ +--------+--------+
| 30 | → 4096 | | 40 | NULL |
+--------+--------+ +--------+--------+
To access "the third element (30)":
- Start at head (address 1000)
- Read pointer → 2048 (one memory read)
- At 2048, read pointer → 3072 (second memory read)
- At 3072, read data (30)
Three memory accesses vs. one calculation.
In modern computers, the difference is even more dramatic due to caching. When you access memory location 1000, the CPU loads not just that byte but an entire 'cache line' (typically 64 bytes). In contiguous storage, this loads several array elements for 'free'. In linked storage, the next node is likely in a completely different cache line, causing a cache miss—which can be 100x slower than a cache hit.
Memory Overhead Analysis:
Consider storing 1000 64-bit integers:
Contiguous (Array):
- Data: 1000 × 8 bytes = 8,000 bytes
- Overhead: ~16 bytes (metadata for size/capacity)
- Total: ~8,016 bytes
- Overhead ratio: 0.2%
Linked (Singly Linked List):
- Data: 1000 × 8 bytes = 8,000 bytes
- Pointers: 1000 × 8 bytes = 8,000 bytes
- Total: ~16,000 bytes
- Overhead ratio: 100%
For small elements, linked storage can more than double memory usage. For large elements (like structs with 100 bytes of data), the pointer overhead becomes negligible.
The Sequence Invariant: Maintaining Order
Regardless of physical organization, every linear data structure must maintain a critical invariant: the logical sequence must remain consistent and accessible. Let's formalize this.
The Sequence Invariant:
At all times, there exists a well-defined path from the first element to the last element that visits every element exactly once in the intended order.
How contiguous structures maintain this invariant:
- The invariant is implicit in the memory layout
- Index 0 is first, index (size-1) is last
- Incrementing the index traverses the sequence
- The path is: memory[0] → memory[1] → ... → memory[n-1]
How linked structures maintain this invariant:
- The invariant is explicit in the link structure
- The head pointer identifies the first element
- Following the 'next' pointers traverses the sequence
- The path is: head → head.next → head.next.next → ... → null
If the sequence invariant is violated, the data structure is corrupted. In arrays, this might mean the size counter doesn't match actual content. In linked lists, this could mean a broken link (pointer to invalid memory) or a cycle (a node pointing back to an earlier node). Such bugs are subtle and dangerous.
Modification and Invariant Preservation:
Every operation that modifies the structure must ensure the invariant remains satisfied:
Insertion:
- Contiguous: Shift elements to make room, increment size counter
- Linked: Allocate new node, update predecessor's link to point to new node, new node's link points to successor
Deletion:
- Contiguous: Shift elements to fill gap, decrement size counter
- Linked: Update predecessor's link to skip deleted node, deallocate node
The discipline of maintaining invariants is central to correct data structure implementation. Bugs occur when operations leave the structure in an inconsistent state—where following the sequence path would skip elements, visit elements twice, or never terminate.
- •Size matches count: Traversing and counting should equal the stored size
- •No broken links: Every pointer (or index) references valid data
- •No cycles (for acyclic structures): Traversal eventually terminates
- •Boundaries respected: First has no predecessor, last has no successor
- •All elements reachable: Every stored element can be reached via traversal
Sequential Access Patterns in Practice
Understanding sequential organization helps us recognize and leverage common access patterns in algorithms. Here are the fundamental patterns:
Linear Scan / Full Traversal
Visiting every element from first to last—the most common pattern. Used for:
- Searching for an element
- Calculating aggregates (sum, max, min)
- Applying a transformation to every element
- Printing or serializing the contents
Both contiguous and linked structures support this pattern well, though contiguous structures benefit from cache efficiency.
Forward Iteration with Early Exit
Traversing until a condition is met, then stopping. Used for:
- Finding the first match
- Validating a property (all elements positive?)
- Finding a boundary or transition point
The key is terminating as soon as the goal is achieved, avoiding unnecessary work.
| Pattern | Description | Use Cases |
|---|---|---|
| Linear Scan | Visit every element start-to-end | Search, sum, transform all |
| Reverse Scan | Visit every element end-to-start | Find last match, reverse operations |
| Early Exit | Stop traversal when condition met | Find first match, validation |
| Two-Pointer | Two indices moving through sequence | Pair finding, partitioning |
| Sliding Window | Fixed/variable window moving along | Subarray/substring problems |
| Skip/Step | Visit every k-th element | Sampling, strided processing |
Reverse Traversal
Visiting elements from last to first. This pattern is:
- Trivial in contiguous structures (decrement index from n-1 to 0)
- Expensive in singly-linked lists (O(n) per element from scratch, or O(n) to first reverse)
- Efficient in doubly-linked lists (follow 'previous' pointers)
This asymmetry is one reason doubly-linked lists exist despite their extra memory cost.
Two-Pointer Techniques
Using two indices or references that move through the sequence in a coordinated way. Classic applications:
- Finding pairs that sum to a target (in sorted data)
- Detecting cycles in linked lists (fast/slow pointers)
- Partitioning arrays around a pivot
Sliding Window
Maintaining a "window" (a contiguous subsequence) that moves through the data. Used for:
- Maximum sum of k consecutive elements
- Substring with certain properties
- Moving averages
These patterns work on any sequential organization but have different performance characteristics depending on whether access is O(1) or O(n).
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.