B2.2 · Book pp. 342–357
Data structures
The way you organize data decides what your program can do quickly. Choosing a good structure often matters more than clever code.
What you need to know
- B2.2.1 Compare static and dynamic data structures.
- B2.2.2 Construct programs that apply arrays and lists.
- B2.2.3 Explain the concept of a stack as Last In First Out.
- B2.2.4 Explain the concept of a queue as First In First Out.
⚠️ Common mistake
“Compare” needs similarities as well as differences. And “stacks are used for undo” earns little. Explain how: actions are pushed, and undo pops the latest one.
B2.2.1 Static and dynamic structures
| Static | Dynamic | |
|---|---|---|
| Size | Fixed when created | Grows and shrinks at runtime |
| Memory | Contiguous, and reserved whether used or not | Allocated as needed, often not contiguous |
| Access | Direct, by index | Depends on the structure. A linked list is sequential |
| Insert in the middle | Slow, as items must shift | Fast in a linked structure, once the position is found |
| Programming | Simpler | Needs more care over size and memory |
| Similarity | Both store and organize many items under one identifier, and both can be traversed with loops | |
✏️ Book corrections
- The book says elements in a dynamic structure cannot be directly accessed, yet later says ArrayLists allow random access. Python lists and Java ArrayLists are dynamic and indexed. It is linked lists that need sequential access.
- It says static structures have no overflow or underflow. They do: its own fixed-size stack has both errors. A fixed size is what makes overflow possible.
- It says dynamic structures waste no memory. Linked structures use extra memory for pointers, so efficient is fairer than no waste.
B2.2.2 Arrays and lists
An array holds many elements under one identifier, reached by an index. Java arrays have a fixed size and one type. Python lists are dynamic, but can be used in the same way.
Parallel arrays are arrays of the same size where one index links related values.
Two-dimensional arrays
A 2D array is a table. The first index is the row and the second is the column, and processing it takes two nested loops.
✏️ Book correction
The book shows a new Java 2D array as empty, with “no initial values”. Java fills a new int array with zeros, as the book said earlier about 1D arrays.
Dynamic lists
grades.append(75) | Add to the end |
grades.insert(0, 64) | Insert at an index |
grades.remove(75) | Remove the first matching value |
grades.pop(0) | Remove by index |
grades.pop() | Remove the last item |
grades.clear() | Empty the list |
len(grades) | Number of items |
Java’s ArrayList offers add, get, set, remove, size and clear. It stores objects, so write ArrayList<Integer> rather than int.
B2.2.3 Stacks
A stack only allows access to its top, like a pile of plates. It is an abstract data structure, built from an array (fixed size) or a linked list.
✏️ Book note
The book’s pop only moves the pointer, and discards the value. A pop normally returns the value it removes, as above.
Uses of stacks
- Undo: actions are pushed, and undo pops the last.
- Browser back: visited pages are pushed, and back pops.
- Interrupts: return addresses are pushed, then popped to resume.
- Recursion: each call’s return address and parameters are stored.
- Reverse Polish: operands are pushed, and an operator pops two.
B2.2.4 Queues
A queue adds at the rear and removes from the front, using two pointers. Operations are enqueue, dequeue, isEmpty, isFull and front.
Uses of queues
- Printing: jobs are served in the order they were sent.
- Keyboard buffer: characters appear in the order they were typed.
- Simulations: checkouts, traffic and car parks.
Try it: reverse a queue using a stack
Dequeue every item and push it. Then pop every item and enqueue it. The order is reversed.
Quick quiz
Exam-style questions
These are written for this site in the style of Paper 2. Answer on paper first, then open the mark points.
-
Compare static and dynamic data structures. [4]
Mark points
- Both store a collection of items under one identifier [1].
- Static has a fixed size, while dynamic can change at runtime [1].
- Static may waste memory, while dynamic uses it as needed [1].
- Static allows direct access, while linked dynamic structures are accessed sequentially [1].
-
Identify the most suitable type and structure to hold 30 student names, in order of arrival. [2]
Mark points
- Strings [1].
- A one-dimensional list or array [1].
-
A 2D list scores has 6 rows and 6 columns. Construct code to output the total of each row. [4]
Mark points
- A loop for each row [1].
- Set total to 0 inside it [1].
- A nested loop adding scores[r][c] [1].
- Output total after the inner loop [1].
-
Outline two uses of a stack in a computer system. [4]
Mark points
- Undo in an editor: actions are pushed [1], and undo pops the latest [1].
- Interrupts: return addresses and values are pushed [1], and popped to resume [1].
-
Outline the difference between a linear queue and a circular queue held in an array. [4]
Mark points
- In a linear queue, removed slots at the front are not reused [1].
- So it can appear full while there is space [1].
- A circular queue wraps the rear pointer back to the start [1].
- This reuses freed slots [1].
-
Evaluate 5 2 * 3 2 + − using a stack. [2]
Mark points
- After 5 2 *: the stack holds 10. After 3 2 +: it holds 10, 5 [1].
- 10 − 5 = 5 [1].