levo

Data Structures

Stacks

  • 3 things to know
  • Component 01

Overview

Recall questions on stacks and their uses, the stack pointer, creating a static stack, push, pop and peek, overflow and underflow, static and dynamic stacks, tracing operations, and why recursion needs a stack rather than a queue.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is a stack?

Key term

Describe the push operation.

Key term

What does peek do, and how is it different from pop?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a stack?

    Model answer

    A last in, first out (LIFO) data structure: items are added and removed at the same end, called the top, so the most recently added item is the first one to come off. Only the top item is accessible; you cannot reach an item lower down without removing the ones above it.

  2. 2

    Give three real uses of a stack in a computer system.

    Model answer

    Storing return addresses, parameters and local variables when subroutines are called, so that execution can return correctly - this is why recursion needs a stack. Saving the contents of registers when an interrupt occurs so the interrupted program can be resumed. Implementing the undo function in applications and the back button in a browser, and evaluating expressions in reverse Polish notation.

  3. 3

    Why does a stack need a pointer, and what does it hold?

    Model answer

    The stack pointer records where the top of the stack currently is, because that is the only position at which pushing and popping happen. A common convention is for it to hold the index of the top item, set to -1 when the stack is empty. Some implementations instead have it point at the next free slot; either is fine as long as you are consistent, because the push and pop steps differ slightly between them.

  4. 4

    How do you create an empty static stack?

    Model answer

    Declare an array of the required maximum size to hold the items, and a stack pointer variable. Set the stack pointer to -1 (or to 0 if your convention is that it indexes the next free slot), which marks the stack as empty. Recording the maximum size lets you detect overflow later.

  5. 5

    Describe the push operation.

    Model answer

    First check whether the stack is full - if the pointer is already at the maximum index, report overflow and do not proceed. Otherwise increment the stack pointer by one and store the new item at that index. The new item is now the top of the stack, and the item previously on top is untouched underneath it.

  6. 6

    Describe the pop operation.

    Model answer

    First check whether the stack is empty - if the pointer is -1, report underflow and do not proceed. Otherwise read the item at the index held in the stack pointer, then decrement the stack pointer by one, and return the item that was read. The data may physically remain in the array, but it is now above the pointer and so is treated as no longer on the stack and will be overwritten by the next push.

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

The 3-step method, run for you

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

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

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.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

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

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

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

A global variable means a variable reachable from anywhere in the program.
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 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.