9618/33/M/J/23 question 11: worked solution (May/June 2023, paper 33)
Show as plain textHide plain text
Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2. Q9 9618/33/M/J/23 original Q11 Structured · 11 marks Worked solution (a) Declare the constant, the array and three integers: CONSTANT MaxSize = 60 DECLARE Queue : ARRAY[1:60] OF STRING DECLARE FrontPointer : INTEGER DECLARE RearPointer : INTEGER DECLARE Length : INTEGER Constant defined correctly [1] Array of 60 STRING items defined correctly (ARRAY[0:59] or ARRAY[1:MaxSize] are also accepted) [1] The three INTEGER variables declared correctly [1] (b) Dequeue works only if the queue is not empty. Take the item from the front, move the front pointer on and reduce the length. If the pointer passes the end, wrap it back to 1. FUNCTION Dequeue RETURNS STRING DECLARE Item : STRING IF Length > 0 THEN Item ← Queue[FrontPointer] FrontPointer ← FrontPointer + 1 Length ← Length - 1 IF Length = 0 THEN CALL Initialise // reset the pointers ELSE IF FrontPointer > MaxSize THEN FrontPointer ← 1 ENDIF ENDIF ELSE OUTPUT "The print queue was empty - error!" Item ← "" ENDIF RETURN Item ENDFUNCTION Any four of these five lines score: IF Length > 0 THEN [1] Item ← Queue[FrontPointer] [1] FrontPointer ← FrontPointer + 1 [1] Length ← Length - 1 [1] Other points that also score: frontPointer ← 1 inside the wrap-around test. (c) Use two stacks so the second stack can reverse the order of the first. Write any four of these points: Two stacks are needed so that the second stack can reverse the order of the first [1] Stack 1 works as the queue, with the newest items at the bottom, and stack 2 is empty [1] To add an item, pop every item from stack 1 and push it onto stack 2 [1] Push the new item onto one of the stacks [1] Other points that also score: Pop all the items of stack 2 back onto stack 1. KEY IDEA A queue ADT needs a front pointer, a rear pointer and a length, and two stacks can copy queue behaviour by reversing the order twice. TOTAL 11 marks Official mark scheme VIDEO ▶ Topic lesson A lesson on this topic · by rjmcompsci. Tap the picture to open it on YouTube.