Question 5(b)(ii) 2015 · May-June · Paper 43 · 5 marks
Reveal official mark-scheme answer
The answer for this question is in the official mark scheme: open the mark scheme.
Show text version of question
Question 5
Part (b)
(ii)
Question 5
A stack Abstract Data Type (ADT) has these associated operations:
- create stack
- add item to stack (push)
- remove item from stack (pop)
The stack ADT is to be implemented as a linked list of nodes.
Each node consists of data and a pointer to the next node. Using pseudocode, a record type, Node, is declared as follows:
TYPE Node DECLARE Name : STRING DECLARE Pointer : INTEGER ENDTYPE
The statement
DECLARE Stack : ARRAY[1:10] OF Node
reserves space for 10 nodes in array Stack.
(ii)
The algorithm for adding a name to the stack is written, using pseudocode, as a
procedure with the header
PROCEDURE Push (NewName)
Where NewName is the new name to be added to the stack. The procedure uses the variables as shown in the identifier table.
Identifier Data type Description Stack Array[1:10] OF Node NewName STRING Name to be added FreePointer INTEGER Pointer to next free node in array TopOfStackPointer INTEGER Pointer to first node in stack TempPointer INTEGER Temporary store for copy of FreePointer
PROCEDURE Push(BYVALUE NewName : STRING) // Report error if no free nodes remaining IF FreePointer = 0 THEN Report Error ELSE // new name placed in node at head of free list Stack[FreePointer].Name NewName ← // take a temporary copy and // then adjust free pointer TempPointer ← FreePointer FreePointer ← Stack[FreePointer].Pointer // link current node to previous top of stack Stack[TempPointer].Pointer ← TopOfStackPointer // adjust TopOfStackPointer to current node TopOfStackPointer ← TempPointer ENDIF ENDPROCEDURE
Question 13
Complete the pseudocode for the procedure Pop. Use the variables listed in the identifier table.
PROCEDURE Pop()
// Report error if Stack is empty
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
OUTPUT Stack [.....................................................................................].Name
// take a copy of the current top of stack pointer
..........................................................................................................................................................
// update the top of stack pointer
..........................................................................................................................................................
// link released node to free list
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
ENDPROCEDURE
Question 14
Why this question appears here
91% assigned-topic match · Data types and structures › Introduction to abstract data types
Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.
Permanent source page: Educate A Change · From 9608_s15_qp_43.pdf
