Home›Study Resources›Computer Science›
What is a stack data structure?
Computer Science
A-Level

What is a stack data structure?

A stack is a data structure that works on the principle last in, first out (LIFO). Items are added (push) and removed (pop) only from the top. A top pointer tracks the most recent item, and peek returns it without removing it. Stacks are used for subroutine calls, undo features and evaluating expressions. Example: Start with an empty stack. Push 5, push 8, pop, push 3. What is on top? Push 5: [5] Push 8: [5, 8] Pop removes 8: [5] Push 3: [5, 3] Answer: 3 is on top.

A stack is an abstract data type with a single access point, the top. At A Level, you should be able to describe push, pop, peek, isEmpty and isFull, trace a sequence of operations and explain stack overflow and underflow. Stacks can be implemented with an array and a top pointer.

Worked example

Starting with an empty stack, perform: push 5, push 8, pop, push 3. Which item is on top, and what was popped?

  1. Push 5: the stack is [5] and top points to 5.
  2. Push 8: the stack is [5, 8] and top points to 8.
  3. Pop: remove the top item, 8. The stack is [5].
  4. Push 3: the stack is [5, 3] and top points to 3.

Answer: 8 was popped and 3 is now on top.

Reviewed by

Want one-to-one support?

One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.

Find a Tutor

Learn face to face in Glasgow

Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.

See the centre