LambdaLabTM
Computer Science · Class 12 · Data Structures
data structuresstack⏱️ 9 min read

What is a Stack?

Put a plate on a pile, then another, then another. When you want a plate back, you take the one on top — the last one you put down. You cannot pull one out from the bottom without the whole pile falling over. That pile is a stack, and this chapter is that one rule, written in Python.

1The definition

A stack is a linear data structure in which items are added and removed at one end only. That end is called the top.

Linear means the items sit one after another — here, one on top of another. One end only is the important part. A list has a front, a back and a middle, and you can work at all of them. A stack has a single opening.

2Try to break the rule

🍽️ A pile of plates

Add plates, take plates, and try to pull one out from the middle.

Tap the top plate to take it off. Then try tapping one lower down.

went on, in this orderABC
came off, in this ordernothing yet

Take all the plates off and look at the two rows. Whatever order the plates went on, they come off in the reverse order. Nobody arranges that. It is simply what happens when there is only one open end.

3LIFO — Last In, First Out

Last In, First Out

The item added most recently is the first one to be removed.

Some books say FILO — First In, Last Out. It is the same rule seen from the other end: the first item put on is the last to come off, because everything else is sitting on top of it. Either name can appear in an exam, and both describe a stack.

put on, in this order
A → B → C
taken off, in this order
C → B → A
A stack reverses things
Put A, B, C in and you get C, B, A out. When a question's expected output is its input written backwards, a stack is usually the reason.

4Stacks you already use

🍽️ Plates in a canteen

The clean plate put down last is the first one picked up.

↩️ The Undo button

Ctrl+Z undoes your most recent change first, then the one before it, and so on backwards.

⬅️ The browser Back button

It takes you to the page you visited most recently, not the first page you opened.

🎾 Tennis balls in a tube

The tube is closed at the bottom, so the ball dropped in last is the first to come out.

📚 Notebooks on a desk

The notebook put on the pile last is the one on top, so it gets picked up first.

💍 Bangles on a wrist

The bangle put on last is nearest the hand, so it has to come off first.

5Six words to know

Top

The one end where items go on and come off. The item sitting there is the top item.

Push

Putting a new item on top of the stack.

Pop

Taking the top item off the stack.

Peek

Looking at the top item without taking it off. Also called peep, or top.

Empty stack

A stack with no items on it. There is nothing to pop.

Underflow and overflow

Trying to pop from an empty stack, and trying to push onto a full one.

Push and pop get the next lesson to themselves, with a stack you can drive.

6Is there a stack in Python?

No. Python has list, tuple and dict, but there is no stack type. A stack is an idea — a rule about how items go in and come out.

To use one in a program, you take an ordinary list and only ever use it by that rule. That is what the syllabus means by implementation of stack using list, and it has a group of lessons of its own, straight after push and pop.

7Recap

One open end

Items are added and removed only at the top.

LIFO

Last In, First Out. FILO is the same rule said the other way round.

It reverses

A, B, C in means C, B, A out.

An idea, not a type

In Python, a stack is a list used by the stack's rule.

Quick Check

A, B, C and D are pushed onto an empty stack in that order. Which item comes off first?

Quick Check

What does LIFO stand for?

Quick Check

Which of these does NOT behave like a stack?

Quick Check

A stack holds five items. Which of them can be removed right now?