AQA A-Level Computer Science 7517
Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation
Fundamentals of Data Representation
Fundamentals of Computer Systems
Fundamentals of Computer Organisation and Architecture
Fundamentals of Communication and Networking
Consequences of Uses of Computing
Systematic Approach to Problem Solving
Fundamentals of Databases
Big Data
Fundamentals of Data Structures
Stacks
- 2 things to know
- Paper 1
Overview
Recall questions on push, pop and peek, what the top pointer holds, testing for empty and full, and tracing a sequence of operations on an array-based stack.
What you need to know
Answer each one before you reveal it.
What is meant by stack overflow and stack underflow?
Questions from this subtopic
Each one with the model answer underneath.
- 1
What is a stack?
Model answer
An abstract data type in which items are added and removed at the same end, called the top. It is a last in, first out (LIFO) structure: the most recently added item is the first one to be removed.
- 2
What does the top pointer of a stack hold, and how is a stack in an array of maxSize elements initialised?
Model answer
The top pointer holds the index of the item currently on top of the stack. It is initialised to -1, which means the stack is empty because no item has been pushed yet. The first item pushed raises the top pointer to 0 and is stored at index 0.
- 3
Describe the push operation.
Model answer
Push adds an item to the top of the stack. First test whether the stack is full; if it is, report stack overflow and do not add. Otherwise increase the top pointer by one and store the new item at the array position the top pointer now gives.
- 4
Describe the pop operation.
Model answer
Pop removes the item at the top of the stack and returns it. First test whether the stack is empty; if it is, report stack underflow and do not remove. Otherwise take the item at the position given by the top pointer, then decrease the top pointer by one so that it points at the item beneath.
- 5
What does peek (or top) do, and how does it differ from pop?
Model answer
Peek hands back the value on top without taking it off, so the stack is unchanged and the top pointer stays put. Pop also returns the top value but removes it, decreasing the top pointer by one. Peek must still test for an empty stack first, because there is no value to return if the stack is empty.
- 6
How do you test a stack for empty and for full?
Model answer
The stack is empty when the top pointer is -1, that is when nothing has been pushed or everything pushed has been popped. A stack held in an array is full when the top pointer has reached the last index, that is when top = maxSize - 1.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Fundamentals of Data Structures, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.