Full Marks

A-level Computer Science Year 12 · Topic 5

Further data structures

Further data structures gives you more powerful ways to organise data for different problems.

We will use circular and priority queues, multi-dimensional arrays, records, tuples, dictionaries and vectors.

Then we build linked lists, binary search trees, graphs and hash tables, and compare their strengths.

In this topic

  1. 5.1Queue variants and multi-dimensional arrays: circular queues, priority queues, 2D and 3D arraysFree below · 25 questions
  2. 5.2Records, tuples, vectors, dictionaries and user-defined data typesOpen in Full Marks · 26 questions
  3. 5.3Linked listsOpen in Full Marks · 28 questions
  4. 5.4Trees and binary search treesOpen in Full Marks · 68 questions
  5. 5.5Graphs: adjacency matrices and adjacency listsOpen in Full Marks · 22 questions
  6. 5.6Hash tables and hashingOpen in Full Marks · 22 questions

5.1 · Free notes

Queue variants and multi-dimensional arrays: circular queues, priority queues, 2D and 3D arrays

Diagram: Queue variants and multi-dimensional arrays: circular queues, priority queues, 2D and 3D arrays

Key points

In more detail

Practice

Queue variants and multi-dimensional arrays: circular queues, priority queues, 2D and 3D arrays: 10 questions

Pick an answer to check it, or open the answer for the explanation.

1. A circular queue has 8 locations (0-7). The rear pointer is 7 and an item is added. Which expression gives the new rear?

  1. rear - 1, giving 6
  2. rear + 1, giving 8
  3. (rear + 1) MOD 8, giving 0
  4. (rear + 1) DIV 8, giving 1
Show answer

C: (rear + 1) MOD 8, giving 0. MOD wraps the pointer: (7 + 1) MOD 8 = 0, so the item goes in location 0 (provided the queue is not full). Plain rear + 1 would go past the end of the array.

2. What is the main advantage of a circular queue over a simple linear queue held in a fixed-size array?

  1. Freed locations at the front can be reused
  2. The queue can never become full
  3. Items can be removed from either end
  4. Items are automatically sorted by priority
Show answer

A: Freed locations at the front can be reused. Wrapping the pointers round lets locations vacated by dequeues be reused, so space is not wasted. The queue still has a fixed maximum size and is still FIFO.

3. A circular queue uses locations 0-4. Front pointer = 3 and rear pointer = 1 (rear points to the last item). How many items are stored?

  1. 3
  2. 4
  3. 5
  4. 2
Show answer

B: 4. Items occupy locations 3, 4, 0 and 1, which is 4 items. Calculation: (1 - 3) MOD 5 + 1 = 3 + 1 = 4.

4. A priority queue (1 = highest priority; ties served first come first served) receives A(2), B(1), C(2), D(3) in that order. What is the dequeue order?

  1. B, A, C, D
  2. B, C, A, D
  3. A, B, C, D
  4. D, A, C, B
Show answer

A: B, A, C, D. B has the highest priority (1) so leaves first. A and C share priority 2, and A arrived first. D has the lowest priority so leaves last.

5. Which situation is the best use of a priority queue?

  1. Undoing the last action in a word processor
  2. Reversing the characters in a string
  3. Storing the positions of pieces on a chessboard
  4. Hospital patients being seen in order of urgency
Show answer

D: Hospital patients being seen in order of urgency. Items are served according to importance, not just arrival time, which is a priority queue. Undo and reversing use a stack; a chessboard uses a 2D array.

6. A 3D array holds readings for 3 floors × 4 rooms × 5 sensors. How many elements does it contain?

  1. 47
  2. 60
  3. 20
  4. 12
Show answer

B: 60. The number of elements is the product of the dimensions: 3 × 4 × 5 = 60. Adding the dimensions (12) is a common error.

7. board = [[1,2,3],[4,5,6],[7,8,9]] is a 2D array with zero-based indices, row first. What is board[2][1]?

  1. 4
  2. 2
  3. 8
  4. 6
Show answer

C: 8. board[2] is the third row [7,8,9]; index 1 of that row is 8. board[1][2] would be 6.

8. Using board = [[1,2,3],[4,5,6],[7,8,9]], a loop adds board[r][2] for r from 0 to 2. What total results?

  1. 12
  2. 15
  3. 6
  4. 18
Show answer

D: 18. The loop adds the last column: 3 + 6 + 9 = 18. Row 1 would give 15, column 0 gives 12 and row 0 gives 6.

9. Which statement about a circular queue implemented in an array is correct?

  1. Dequeuing shifts every remaining item one place left
  2. The front pointer always has a smaller value than the rear
  3. The rear pointer can have a smaller value than the front
  4. A stack is used to make the pointers wrap round
Show answer

C: The rear pointer can have a smaller value than the front. Once the rear wraps past the end of the array it is numerically below the front, e.g. front = 4, rear = 1. No shifting is needed, which is why it is efficient.

10. A circular queue has maximum size 6 and its count variable is 6. A program tries to enqueue another item. What should happen?

  1. The item is stored in location 6
  2. The front pointer is reset to 0 and the item added
  3. The oldest item is silently overwritten
  4. A queue full (overflow) error is reported
Show answer

D: A queue full (overflow) error is reported. count = maxSize means the queue is full. Adding an item would overwrite unread data, so the program must check for this and report overflow.

Keep going in Full Marks

Full Marks has 25 questions on Queue variants and multi-dimensional arrays: circular queues, priority queues, 2D and 3D arrays (15 more than here), set out in practice levels. You also get notes and 3D models for all 6 subtopics of Further data structures, plus games, timed exams and past papers.

On your exam board

All A-level Computer Science Year 12 topics