Algorithms and programming June 2025, Question 3 17 marks
A program is designed to use a stack data structure.
The current contents of part of the stack and its current pointer value are shown:
pointer = 2
Show the contents of the stack and its pointer value after the data items "t" and "s" are inserted in the order given.
Reveal mark scheme
The stack is stored using a 0-based 1-dimensional array called theStack with 20 elements.
The function push() stores its parameter into the next space in the stack if it is not full. The function returns true if it is successfully stored and returns false if the stack is full.
Complete the function push() using pseudocode or program code.
Reveal mark scheme
A pointer value of-1 indicates that the stack is empty.
The function pop() returns the next element in the stack. If the stack is empty it returns "false". The function updates the pointer value when appropriate.
Write the function pop() using pseudocode or program code.
Reveal mark scheme
A program is designed to reverse a set of data using the stack.
The main program takes 10 characters separately as input from the user and uses the function push() to insert these into the stack.
Once all 10 characters have been inserted into the stack, the program then uses the function pop() to output all 10 characters from the stack.
Write the main program to achieve this using pseudocode or program code.
Reveal mark scheme