Skip to content

3.1   Classifying data structures

Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. We can classify them from two perspectives: logical structure and physical structure.

3.1.1   Logical structure: linear and nonlinear

Logical structure describes the relationships between data elements. In arrays and linked lists, elements follow a sequence, a linear relationship. A tree arranges elements in levels from top to bottom, forming ancestor-descendant relationships. A graph consists of nodes and edges that can express complex networks of relationships.

As the figure shows, logical structures fall into two broad categories: linear and nonlinear. A linear structure arranges elements logically along a single sequence. A nonlinear structure arranges them in some other way.

  • Linear data structures include arrays, linked lists, stacks, and queues. Successive elements have a one-to-one relationship.
  • Nonlinear data structures include trees, heaps, and graphs.

Trees and networks are two common forms of nonlinear structure.

  • Tree structures, including trees and heaps, express one-to-many relationships between elements.
  • Network structures, such as graphs, express many-to-many relationships between elements.

A hash table looks up values by key. The figure shows this mapping separately.

Figure 3-1   Linear and nonlinear data structures

Description of Figure 3-1: Logical relationships in data structures

Compare four logical relationships: sequences, parent-child hierarchies, network connections, and key-value mappings.

The left column shows linear structures and the right column nonlinear structures. The hash table appears separately below both columns to illustrate a key-value mapping.

Examples of the structures and their connections
StructureColumn in the figureElements and relationships shown
ArrayLinearOrdered 1, 3, 2, 5, 4
Linked listLinear1→3→2→5→4
StackLinearFrom the open end to the closed end: 1, 3, 2, 5, 4. Both push and pop operate at the open end
QueueLinearFrom the removal end to the insertion end: 1, 3, 2, 5, 4. Push enqueues at one end and pop dequeues at the other
TreeNonlinearRoot 1 has children 2 and 3, node 2 has children 4 and 5, and node 3 has children 6 and 7, in left-to-right order. Seven nodes span three levels
GraphNonlinearFive vertices numbered 1 through 5, with seven undirected edges: 1–2, 1–3, 2–3, 2–4, 2–5, 3–5, and 4–5
HeapNonlinearRoot 1 has left child 3 and right child 2. Node 3 has left child 5 and right child 4. Five nodes span three levels
Hash tableShown separately across both columnsKeys a, b, and c map to values 1, 2, and 3, respectively

Linear structures have a sequential order, but differ in where operations can take place. Trees and heaps form parent-child hierarchies, while a graph allows a vertex to have multiple neighbors. These are logical relationships. The underlying storage can vary for the same relationship, and a hash table can combine arrays, linked lists, or trees.

3.1.2   Physical structure: contiguous and noncontiguous

When an algorithm runs, the data it is processing is stored mainly in memory. Each black chip on the memory module below contains a region of memory. Imagine memory as a huge Excel spreadsheet, with each cell able to hold a fixed amount of data.

The system uses a memory address to access the data at a particular location. As shown below, the computer assigns numbers to the cells according to a set of rules so that each location has a unique address. A program uses these addresses to access the data in memory.

Figure 3-2   Memory module, memory space, and memory addresses

Description of Figure 3-2: Accessing memory by address

Add the column offset to the row's starting address to locate a memory cell and read its data.

On the left is a vertical memory module. Two gray lines connect its black chip area to the memory grid on the right. The physical chips and the dark cells marking used space in the grid represent different levels of detail.

The grid has five rows and eight columns. The rows start at 0x0000, 0x0010, 0x0020, 0x0030, and 0x0040, with column offsets 00 through 07. Ellipses at the right and bottom indicate additional cells. Light cells are available and dark cells are in use. An available cell does not necessarily contain 0.

Two occupied memory cells in the diagram
Full addressRow starting addressColumn offsetStored data
0x00120x0010021
0x00240x0020042

Add a row's starting address and a column offset to locate a cell. For example, 0x0020 plus offset 04 gives 0x0024, which holds the value 2. The two reads below the grid correspond to the two rows in the table. An address identifies a location, and the data is the content stored there. The grid illustrates addressing rather than the chip's physical circuit layout.

Tip

The spreadsheet is a simplified analogy. Actual memory systems are more complex, involving address spaces, memory management, caching, and both virtual and physical memory.

Memory is a resource shared by all programs. A block in use by one program is usually unavailable to other programs at the same time. Memory is therefore an important consideration when designing data structures and algorithms. For example, an algorithm's peak memory use should not exceed the system's remaining free memory. If no large contiguous block is available, the chosen structure must be able to use scattered memory.

As shown below, physical structure describes how data is stored in computer memory. Storage may be contiguous, as in an array, or noncontiguous, as in a linked list. The underlying layout determines how data is accessed, updated, inserted, and removed. The two layouts have complementary strengths in time and space efficiency.

Figure 3-3   Contiguous and noncontiguous storage

Description of Figure 3-3: Memory layouts of arrays and linked lists

The same logical sequence can occupy contiguous storage or be linked by pointers between scattered nodes.

Each panel shows a grid with five rows and six columns. Rows are numbered from top to bottom and columns from left to right, both starting at 1. On the left, the array stores 1, 3, 2, 5, 4 consecutively in the first five cells of row 2. On the right, the linked list nodes are scattered throughout the grid. Each node occupies two adjacent cells, one for the value and one for the pointer.

Light gray cells indicate available space. Array cells are green, linked list value cells are teal, and pointer cells are light blue. The table below lists the nodes' positions in traversal order.

Linked list node positions and successors, with rows and columns numbered from 1
Node valueValue cell positionPointer cell positionSuccessor
1Row 2, column 2Row 2, column 33
3Row 3, column 3Row 3, column 42
2Row 5, column 2Row 5, column 35
5Row 5, column 5Row 5, column 64
4Row 1, column 5Row 1, column 6No outgoing arrow shown

Adjacent positions give an array its order, while a linked list is traversed by following successor pointers. Although node 4 appears at the top, it comes last in 1 → 3 → 2 → 5 → 4. Its pointer value is unspecified. Contiguous storage needs no separate successor pointer for each element. Scattered nodes can use fragments of free space but require extra storage for pointers.

All data structures are implemented using arrays, linked lists, or a combination of the two. For example, either an array or a linked list can implement a stack or queue, while a hash table may combine both.

  • Arrays can implement stacks, queues, hash tables, trees, heaps, graphs, matrices, tensors (arrays of dimension \(\geq 3\)), and more.
  • Linked lists can implement stacks, queues, hash tables, trees, heaps, graphs, and more.

A linked list can still change length at runtime after initialization, so it is called a dynamic data structure. An array has a fixed length after initialization, so it is called a static data structure. Reallocating memory can nevertheless give an array some flexibility in length.

Tip

If you find physical structure difficult to visualize, try reading the next chapter and then returning to this section.

Share your thoughts, questions, or suggestions in the comments.