OCR H446, section 1.4.2 (b)
This page describes what each structure is, how it is organised, and what it is used for. How to create, traverse, add and remove data is in 1.4.2 (c).
Linked list
A linked list is a collection of nodes. Each node holds a data item and a pointer to the next node. The nodes can be anywhere in memory.
- A dynamic structure: it grows and shrinks one node at a time, and uses memory only as needed.
- Inserting or removing an item in the middle only needs the pointers to be changed. No other items have to be moved (unlike an array).
- Searching is slow: to reach an item, you must follow the pointers from the start. There is no direct access by index.
- Needs extra memory for the pointers.
- A doubly linked list has pointers to both the next and previous nodes, so it can be traversed in either direction.
- Used for: lists that change often, and as the basis of stacks, queues and other structures.
Graph
A graph shows relationships. It is made of vertices (nodes) and edges (arcs) that join them.
- Undirected graph: edges have no direction, so a link works both ways (such as friendship).
- Directed graph (digraph): each edge has a direction, shown by an arrow (such as "follows" on social media, or a one-way street).
- A weighted graph gives each edge a value (a distance or cost).
- Stored as an adjacency matrix (a 2D array) or an adjacency list (for each vertex, the list of its neighbours).
- Used for: maps and routes (shortest path), networks, social networks, the web (pages and links), and scheduling.
Stack
A stack is a last in, first out (LIFO) structure. Items are added to and removed from the same end, the top.
- Operations: push (add to the top), pop (remove from the top), peek (look at the top), and tests for empty and full.
- Used for: the call stack of subroutines, undo features, the browser back button, reversing data, evaluating expressions, and backtracking.
Queue
A queue is a first in, first out (FIFO) structure. Items are added at the rear and removed from the front.
- Operations: enqueue (add), dequeue (remove), and tests for empty and full.
- A circular queue reuses the space at the front of an array as items are removed, and a priority queue serves the highest priority item first.
- Used for: printer queues, keyboard buffers, scheduling tasks, and breadth-first searching.
Tree
A tree is a connected graph with no cycles, usually drawn with a root at the top. Each node has child nodes below it, and (except the root) one parent.
- A node with no children is a leaf.
- Used for: file systems (folders within folders), organisation charts, decision trees, syntax trees in compilers, and game trees.
Binary tree
A binary tree is a tree in which each node has at most two children, the left and right child.
Binary search tree (BST)
A binary search tree is a binary tree that is ordered: for every node, everything in its left subtree is smaller, and everything in its right subtree is larger.
- Searching is fast: at each node you go left or right, ignoring half of what is left, so a balanced tree takes about steps.
- An in-order traversal (left, node, right) outputs the values in sorted order.
- Used for: fast searching and sorting of data, and dictionaries.
- If the tree is unbalanced (for example values inserted in sorted order), it becomes a chain, and searching becomes slow.
Hash table
A hash table stores data in an array, and uses a hash function to turn a key into the position (index) where the item is stored.
- Finding, adding and removing an item is very fast on average, because the position is calculated directly, with no searching.
- A collision happens when two keys give the same position. Three ways to handle it:
- Linear probing: move through the table one space at a time until a free space is found, and store the item there.
- Chaining: each location points to a linked list, and the new item is added to the list at its location.
- Overflow area: items that collide are stored in a separate area of the table.
- A good hash function spreads the keys evenly, and is quick to calculate.
- The data isn't kept in order.
- Used for: dictionaries, database indexes, caches, and symbol tables in compilers.
Comparison
| Structure | Order of access | Key feature | Time to find an item | Typical use |
|---|---|---|---|---|
| Linked list | Follow pointers | Easy insert and delete | O(n): follow the pointers from the start | Lists that change |
| Graph | Follow edges | Models relationships | Maps, networks | |
| Stack | Last in, first out | Access one end | Undo, call stack | |
| Queue | First in, first out | Add at one end, remove at the other | Printing, scheduling | |
| Tree / BST | From the root | Hierarchy, or ordered for fast search | O(log n) for a balanced BST: each step halves what is left | Files, sorted data |
| Hash table | By key | Very fast lookup | O(1): the position is calculated directly | Dictionaries, caches |
Worked example
Task: which structure suits each?
- A phone's list of recent calls, where the newest is added and the oldest is dropped. A queue (first in, first out).
- An editor's "undo" history. A stack: the latest change is undone first.
- Finding a customer's record quickly by their ID. A hash table (or a BST).
- Finding the shortest route between towns. A weighted graph.
- A music playlist where songs are often inserted in the middle. A linked list.
- A family tree. A tree.
Exam tip: give the defining feature of each (LIFO, FIFO, nodes with pointers, vertices and edges, root with children, key to index with a hash function), and link the structure to a use that fits.
Check yourself
What is the main advantage of a linked list over an array?
It can grow and shrink easily, and items can be inserted or removed by changing pointers, without moving other items.
What is the difference between a stack and a queue?
A stack is last in, first out, with items added and removed at the same end. A queue is first in, first out, with items added at the rear and removed from the front.
What property makes a binary search tree quick to search?
Everything in the left subtree is smaller and everything in the right is larger, so each comparison rules out about half of the remaining items.
What is a collision in a hash table?
When two different keys produce the same index, so they would be stored in the same position.