levo

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.

Your recallNot started
Process

What does peek (or top) do, and how does it differ from pop?

Key term

What is meant by stack overflow and stack underflow?

Questions from this subtopic

Each one with the model answer underneath.

  1. 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. 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. 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. 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. 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. 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.

1Closed book
2Open book
3Closed book

See a huge jump in your memorisation

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It trains answer structure

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

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.