Question bank › A-level › A-level Computer Science › Fundamentals of data structures

Queues and stacks

10 questions45 marksabout 55 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Standard5 marks

A circular queue is implemented using an array Q with six elements, indexed 0 to 5, and the variables front, rear and size. Initially the queue is empty with front = 0, rear = -1 and size = 0.

When an item is added, rear is updated using rear ← (rear + 1) MOD 6; when an item is removed, front ← (front + 1) MOD 6.

The following operations are carried out in order:

Enqueue(A), Enqueue(B), Enqueue(C), Enqueue(D), Dequeue, Dequeue, Enqueue(E), Enqueue(F), Enqueue(G)

(a) State the values of front, rear and size after these operations. [3]

(b) State the item stored at index 0 of the array. [1]

(c) Explain why a circular queue is a more efficient use of memory than a linear queue implemented in a fixed-size array. [1]

2Standard5 marks

A stack is implemented using an array and a top pointer. The array has indices 0 to 4. Initially the stack is empty and the top pointer is −1.

The following operations are carried out in order. Each value removed by POP is output.

PUSH(7), PUSH(3), POP, PUSH(9), PUSH(4), POP, POP, PUSH(2), PUSH(8)

(a) State the values that are output, in order. [2]

(b) State the contents of the stack, from bottom to top, and the value of the top pointer after all of the operations. [2]

(c) Describe the check that the PUSH operation should make before adding an item, and what should happen if the check fails. [1]

3Core3 marks

Stacks and queues are both abstract data types.

(a) Explain the difference between the order in which items are removed from a stack and the order in which they are removed from a queue. [1]

(b) A stack supports the operations Push, Pop and Peek. Describe how the Peek operation differs from the Pop operation. [1]

(c) A web browser stores the addresses of pages a user has visited so that the Back button returns to the previous page. State, with a reason, whether a stack or a queue should be used. [1]

Stuck on Queues and stacks?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Core4 marks

A linear queue of customer names is implemented using a one-dimensional array Q with five elements, indexed 0 to 4, and two pointers. The diagram shows the current state of the queue.

When an item is added, RearPointer is increased by 1 and the item is stored at Q[RearPointer]. When an item is removed, the item at Q[FrontPointer] is returned and FrontPointer is increased by 1.

The following operations are carried out in order:

Enqueue("Liv"), Dequeue, Enqueue("Sam"), Dequeue, Enqueue("Ade")

(a) State the values of FrontPointer and RearPointer after these operations. [2]

(b) The operation Enqueue("Zoe") is now attempted. Explain why it fails even though fewer than five customers are in the queue. [1]

(c) Describe one way that a linear queue could be managed so that the space at the start of the array can be reused. [1]

QKai[0]Mo[1][2][3][4]↑FrontPointer = 0↑RearPointer = 1
5Standard4 marks

A printer in a school office uses a priority queue for print jobs. Each job is given a priority from 1 (highest) to 3 (lowest). Jobs with the same priority are printed in the order in which they arrived.

Six jobs arrive in the following order before any are printed:

JobABCDEF
Priority231213

(a) State the order in which the six jobs will be printed. [2]

(b) The priority queue is stored in an array, kept in the order in which the jobs will be removed. Describe how adding an item to this priority queue differs from adding an item to an ordinary (linear) queue. [2]

6Standard4 marks

A stack of integers is implemented using an array Stack, indexed from 0 to MaxSize − 1, and an integer variable TopPointer. When the stack is empty, TopPointer is −1.

Write, using pseudo-code, a function Pop that removes and returns the item at the top of the stack. Your function should deal with an attempt to pop from an empty stack by outputting an error message.

You may assume Stack and TopPointer are global. [4]

7Hard5 marks

A circular queue is implemented using an array Q indexed 0 to 7 (MaxSize = 8), a FrontPointer (index of the first item) and a RearPointer (index of the last item). No separate count of items is stored.

(a) At one point FrontPointer = 5 and RearPointer = 2. Calculate the number of items in the queue. [1]

(b) Write an expression, using FrontPointer, RearPointer and MaxSize, that gives the number of items in the queue whether or not the rear has wrapped round to the start of the array. [2]

(c) When the queue is completely full FrontPointer = 3 and RearPointer = 2. Explain why these pointer values alone cannot be used to decide whether the queue is full, and describe how the implementation could be changed to solve this. [2]

8Hard5 marks

The algorithm below uses a queue Q and a stack S. S is initially empty. Q initially contains, from front to rear: 4, 9, 2, 11, 7, 15

WHILE NOT Q.IsEmpty()
  x ← Q.Dequeue()
  IF S.IsEmpty() OR x > S.Peek() THEN
    S.Push(x)
  ELSE
    OUTPUT x
  ENDIF
ENDWHILE
WHILE NOT S.IsEmpty()
  OUTPUT S.Pop()
ENDWHILE

(a) State all of the values output by the algorithm, in order. [3]

(b) State the maximum number of items held in S at any time. [1]

(c) Explain why the first WHILE loop uses S.Peek() rather than S.Pop() in the comparison. [1]

9Standard4 marks

A keyboard buffer stores the characters typed by a user until the program reads them.

(a) Explain why a queue, rather than a stack, is used for a keyboard buffer. [1]

(b) The buffer could be implemented as a static data structure (a fixed-size array) or as a dynamic data structure (e.g. a linked list using memory from the heap).

Give one advantage and one disadvantage of using a dynamic implementation. [2]

(c) Explain why the IsFull test is essential for the static implementation. [1]

10Stretch6 marks

A queue can be implemented using two stacks, S1 and S2.

  • Enqueue(x): push x onto S1.
  • Dequeue: if S2 is empty, repeatedly pop an item from S1 and push it onto S2 until S1 is empty. Then pop the top item of S2 and output it.

The diagram shows the current contents of the two stacks.

The following operations are now carried out in order:

Dequeue, Enqueue(9), Dequeue, Dequeue, Enqueue(2), Dequeue

(a) State the four values that are output, in order. [2]

(b) State the contents of S1 and S2, from bottom to top, immediately after the second Dequeue has been completed. [2]

(c) Explain why items must only be moved from S1 to S2 when S2 is empty. [2]

Stack S137Stack S24(bottom of each stack at the base)

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session