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

Push and Pop

A stack has two main operations, and the syllabus names both. Push puts an item on top. Pop takes the top item off and hands it back. Two smaller ones come with them — look at the top without taking it, and ask whether the stack is empty — and board questions use all four.

1Push — put an item on top

push(item)

The item goes on top. Nothing already on the stack moves.

before
1020
push(30) →
after
102030

30 did not go to the bottom or into the middle. There is only one place a push can put anything, and it is the top.

2Pop — take the top item off

pop()

The top item comes off, and you are handed it.

before
102030
pop() →hands back 30
after
1020

Two things happen in one operation: the stack gets one item shorter, and you receive the item that was on top. A program usually wants that item — to print it, or use it.

Pop takes no item
You never tell pop which item to take. There is only ever one it can take, so it needs no information from you.

3Peek and isEmpty — the two that change nothing

Peek hands back the top item and leaves it where it is. Books also call it peep or top — the 2024-25 CBSE sample paper asks for a function named peep. isEmpty answers one question: is there anything on the stack? It gives back True or False.

Four operations side by side
OperationWhat changes on the stackWhat you get back
push(item)one more item, on topnothing
pop()the top item is removedthe item that was on top
peek()nothingthe top item
isEmpty()nothingTrue or False

Read the table by its columns. Push changes the stack and gives nothing back. Peek and isEmpty give something back and change nothing. Pop is the only one that does both.

4Drive the stack

🥞 Drive the stack yourself

Push, pop, peek, and ask whether it is empty. Then empty it and try to pop anyway.

the stack
10
20
30← top
3 items on it
last operation
nothing run yet

Three items are on the stack already. 30 went in last, so it is on top.

Three things worth trying:

  • Press peek twice. Same answer both times, and the pile does not move.
  • Pop until the stack is empty, then pop once more.
  • Switch on room for only 5 and keep pushing.

5Underflow and overflow

Underflow

Trying to pop (or peek) when the stack is empty. There is nothing to take.

Overflow

Trying to push when the stack is already full. There is no room left.

A program must check before it pops. That is why almost every board question adds a line like “display Underflow when the stack is empty” or “display Stack Empty”. The check is part of the answer, and it carries marks.

Overflow needs a fixed size
A stack can only be full if it has a fixed number of places. A Python list grows every time you add to it, so the stacks you write in Python do not fill up. You will check for underflow in every program. You still need to know what overflow means.

6Trace a run of operations

A common question gives a list of operations and asks what is printed. Assume every pop prints the item it removes. The safe way to answer is a table: one row per operation, and after each one, write down the whole stack. Try the numbers run in your notebook before you press anything.

📝 Trace it, one operation at a time
#operationstack after it (bottom → top)printed
1Push(10)
2Push(20)
3Pop()
4Push(30)
5Push(40)
6Pop()
7Pop()
8Pop()
9Pop()
now

The stack starts empty. Work out each row yourself, then press Next step to check.

printed so far
Point at the top before every pop
The usual slip is popping from the bottom — writing 10 where the answer is 20. Before each pop, put your finger on the last item you wrote in that row. That is the one that comes off.

7Recap

push(item)

Puts the item on top. Gives nothing back.

pop()

Removes the top item and hands it back. The only operation that does both.

peek() and isEmpty()

Look, without changing anything.

Underflow

Popping an empty stack. Always check before you pop.

Overflow

Pushing onto a full stack. Only possible when the stack has a fixed size.

Tracing

One row per operation, and write the whole stack every time.

Quick Check

From bottom to top, a stack holds 5, 9, 2. What does pop() hand back?

Quick Check

Which operation hands back the top item without removing it?

Quick Check

Starting with an empty stack: Push(4), Push(7), Pop(), Push(1), Pop(), Pop(). Every pop prints its item. What is printed?

Quick Check

What is underflow?