What a Stack Is: LIFO
Quick answer A stack allows insertion and deletion at one end only — the top — so the item pushed last is the item popped first, which is the entire meaning of LIFO.
A stack is a linear data structure in which you may insert and delete at one end only. That single open end is called the top. Everything below it is sealed in.
Because the item you put in last is the one sitting on top, it is also the item you can take out first. That is the whole definition: a stack is LIFO — Last In, First Out.
You already use stacks every day without calling them that:
- A pile of steel plates at a canteen counter. You lift the top plate, and a washed plate goes back on top.
- The Back button in a browser. It takes you to the page you visited most recently, not the first one you opened.
- Ctrl+Z in any editor. Undo reverses your latest action first, then the one before it.
- Answer sheets an invigilator collects. The last sheet handed in lies on top of the bundle.
Contrast this with the queue at a Metro ticket counter, where the first person to arrive is served first. That is a different structure, and it is not in your syllabus. Everything in this chapter is LIFO.
The vocabulary the board expects:
| Term | Meaning |
|---|---|
| Top | The only position where an item can be added or removed |
| Push | Insert a new item on the top |
| Pop | Remove the top item and return it |
| Peek (or Top operation) | Read the top item without removing it |
| Underflow | Trying to pop or peek when the stack is empty |
| Overflow | Trying to push when the stack has reached its maximum size |
Worked example. Six operations on a stack of student names. Watch which name comes out first.
# A stack of answer sheets on an invigilator's desk
stack = [] # an empty stack
stack.append("Aarav") # push
stack.append("Diya") # push
stack.append("Kabir") # push
print("Stack now :", stack)
print("Top item :", stack[-1])
print("Popped :", stack.pop())
print("Popped :", stack.pop())
print("Stack now :", stack)
stack.append("Ishaan")
print("Stack now :", stack)
print("Top item :", stack[-1])Real output:
Stack now : ['Aarav', 'Diya', 'Kabir']
Top item : Kabir
Popped : Kabir
Popped : Diya
Stack now : ['Aarav']
Stack now : ['Aarav', 'Ishaan']
Top item : IshaanTrace it operation by operation. In the table the list is written bottom to top, exactly as Python prints it:
| Operation | Stack (bottom to top) | Top |
|---|---|---|
| push Aarav | ['Aarav'] | Aarav |
| push Diya | ['Aarav', 'Diya'] | Diya |
| push Kabir | ['Aarav', 'Diya', 'Kabir'] | Kabir |
| pop returns Kabir | ['Aarav', 'Diya'] | Diya |
| pop returns Diya | ['Aarav'] | Aarav |
| push Ishaan | ['Aarav', 'Ishaan'] | Ishaan |
The proof of LIFO is in the third line of the output. Kabir went in last and came out first. Aarav went in first and was still sitting at the bottom, untouched, at the end. Diya could not be reached until Kabir was removed — in a stack you cannot pull an item out from the middle.
- A stack permits insertion and deletion at one end only; that end is called the top.
- LIFO means Last In, First Out — the most recently pushed item is the first one popped.
- The four operations are push (insert), pop (remove and return), peek (read the top), and the empty test.
- Underflow is popping or peeking an empty stack; overflow is pushing into a full stack.
- Real examples: browser Back, Ctrl+Z undo, a pile of plates, a bundle of collected answer sheets.
