Question 2

OCR Computer Science June 2023 Computer principles (H046-01)AS14 marks

On the specification

(a)(i)
1.4 Data types, data structures and algorithms
(a)(ii)
1.4 Data types, data structures and algorithms2.3 Algorithms
(b)(i)
1.4 Data types, data structures and algorithms2.3 Algorithms
(b)(ii)
2.2 Problem solving and programming2.3 Algorithms
Question text

2 A programmer uses a queue data structure to store data. (a) (i) Tick one box that describes how a queue operates. Last In First Out First In First Out [1] (ii) The figure below shows a queue data structure that contains a list of names. Alex is at the front of the queue. Alex Kofi Ben Sundip Tom The operations that can be used on the queue are: • enqueue() – This will add data that is passed in as a parameter to the queue. • dequeue() – This will return the first element in the queue. Show the contents of the queue after these operations have been performed: enqueue("Charlie") dequeue() enqueue("Ling") dequeue() enqueue("Sara") [2] (b) A stack is another type of data structure. A stack is implemented using these variables: • items – This is used to store an array that contains the data. • top – This is an integer value pointing to the last item of data that was inserted. pop() is one operation that can be performed on a stack. This will remove an item from the top of the stack, or –1 if the stack is empty. (i) Complete the pseudocode function for the pop() operation. function pop() if top == then return –1 else item = items[ ] top = top – return endif endfunction [4] (ii) A function called reverse uses a stack called theStack to reverse data that is passed in as a parameter called name. For example, the name “Jack” would be returned as “kcaJ” by the function. theStack uses these operations which are already defined as global scope in the program: • push() – This will add data that is passed in as a parameter to the stack. • pop() – This will remove and return the item on top of the stack. Write the function reverse so that it: • accepts the name as a parameter • uses push() to add each character in the name to theStack separately • uses pop() to return each character from theStack and add it to a variable called reverseName • outputs the variable reverseName once all characters have been popped from theStack. You should write your function using pseudocode or program code. [7]