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.
push(30) →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.
pop() →hands back 30Two 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.
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.
| Operation | What changes on the stack | What you get back |
|---|---|---|
| push(item) | one more item, on top | nothing |
| pop() | the top item is removed | the item that was on top |
| peek() | nothing | the top item |
| isEmpty() | nothing | True 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
Push, pop, peek, and ask whether it is empty. Then empty it and try to pop anyway.
——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
Trying to pop (or peek) when the stack is empty. There is nothing to take.
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.
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.
| # | operation | stack after it (bottom → top) | printed |
|---|---|---|---|
| 1 | Push(10) | ||
| 2 | Push(20) | ||
| 3 | Pop() | ||
| 4 | Push(30) | ||
| 5 | Push(40) | ||
| 6 | Pop() | ||
| 7 | Pop() | ||
| 8 | Pop() | ||
| 9 | Pop() |
The stack starts empty. Work out each row yourself, then press Next step to check.
7Recap
Puts the item on top. Gives nothing back.
Removes the top item and hands it back. The only operation that does both.
Look, without changing anything.
Popping an empty stack. Always check before you pop.
Pushing onto a full stack. Only possible when the stack has a fixed size.
One row per operation, and write the whole stack every time.
From bottom to top, a stack holds 5, 9, 2. What does pop() hand back?
Which operation hands back the top item without removing it?
Starting with an empty stack: Push(4), Push(7), Pop(), Push(1), Pop(), Pop(). Every pop prints its item. What is printed?
What is underflow?