You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
A data structure is an organised way of storing and managing data so that it can be accessed and modified efficiently. Choosing the right data structure is essential for writing effective programs.
An array is a collection of elements of the same data type, stored in contiguous (consecutive) memory locations. Each element is accessed using an index (a number that indicates its position).
names = ["Alice", "Bob", "Charlie", "Diana"]
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Value | Alice | Bob | Charlie | Diana |
names[0] returns "Alice"names[2] returns "Charlie"A one-dimensional (1D) array is a single list of values:
scores = [85, 92, 78, 90]
A two-dimensional (2D) array is like a table (rows and columns):
grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
grid[0][1] returns 2 (row 0, column 1)grid[2][0] returns 7 (row 2, column 0)Exam Tip: When asked about 2D arrays, think of them as a table. The first index selects the row and the second index selects the column.
A record is a data structure that stores a collection of related fields, which can be of different data types. Records are used to represent a single entity with multiple attributes.
A student record might contain:
| Field | Data Type | Example |
|---|---|---|
| name | String | "Alice Smith" |
| age | Integer | 15 |
| year_group | Integer | 11 |
| String | "alice@school.com" |
In pseudocode, you might access a record like this:
student.name = "Alice Smith"
student.age = 15
student.year_group = 11
student.email = "alice@school.com"
Records differ from arrays because:
A stack is a Last In, First Out (LIFO) data structure. The last item added to the stack is the first one to be removed — just like a stack of plates.
The control flow of stack operations is summarised below:
graph TD
A["Stack operation"] --> B{"Operation type?"}
B -->|Push| C{"Stack full?"}
C -->|Yes| D["Stack overflow error"]
C -->|No| E["Add item to top"]
B -->|Pop| F{"Stack empty?"}
F -->|Yes| G["Stack underflow error"]
F -->|No| H["Remove and return top item"]
B -->|Peek| I["Return top item without removing"]
| Operation | Description |
|---|---|
| Push | Add an item to the top of the stack |
| Pop | Remove and return the item from the top of the stack |
| Peek (or Top) | View the top item without removing it |
| isEmpty | Check if the stack is empty |
| isFull | Check if the stack is full (for fixed-size stacks) |
Starting with an empty stack:
| Operation | Stack (top → bottom) | Returned Value |
|---|---|---|
| Push(5) | [5] | — |
| Push(3) | [3, 5] | — |
| Push(8) | [8, 3, 5] | — |
| Pop() | [3, 5] | 8 |
| Peek() | [3, 5] | 3 |
| Pop() | [5] | 3 |
A queue is a First In, First Out (FIFO) data structure. The first item added to the queue is the first one to be removed — just like a queue of people waiting in a line.
| Operation | Description |
|---|---|
| Enqueue | Add an item to the back (rear) of the queue |
| Dequeue | Remove and return the item from the front of the queue |
| Peek (or Front) | View the front item without removing it |
| isEmpty | Check if the queue is empty |
| isFull | Check if the queue is full |
Starting with an empty queue:
| Operation | Queue (front → back) | Returned Value |
|---|---|---|
| Enqueue(5) | [5] | — |
| Enqueue(3) | [5, 3] | — |
| Enqueue(8) | [5, 3, 8] | — |
| Dequeue() | [3, 8] | 5 |
| Peek() | [3, 8] | 3 |
| Dequeue() | [8] | 3 |
| Feature | Stack | Queue |
|---|---|---|
| Order | LIFO (Last In, First Out) | FIFO (First In, First Out) |
| Add | Push (to top) | Enqueue (to back) |
| Remove | Pop (from top) | Dequeue (from front) |
| Real-world analogy | Stack of plates | Queue at a shop |
Exam Tip: Remember the mnemonics: Stack = LIFO (Last In, First Out), Queue = FIFO (First In, First Out). Be prepared to trace through push/pop or enqueue/dequeue operations on a diagram.
When a program calls a function, the CPU pushes the return address (and local variables) onto a call stack. Consider:
FUNCTION main()
x = square(5)
OUTPUT x
ENDFUNCTION
FUNCTION square(n)
RETURN n * n
ENDFUNCTION
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.