8.1 What is a Data Structure?
You already know several data structures from Class XI: list, tuple, dict, set, str. Each one organises its items in a particular way and offers specific operations:
| Structure | How it is organised | Access |
|---|---|---|
| List | Ordered, mutable | By index |
| Tuple | Ordered, immutable | By index |
| Dictionary | Key → value pairs | By key |
| Set | Unordered, unique | Membership test |
| Stack | Linear, last-in-first-out | Only at the top |
Beyond these, computer scientists define many more — queue, linked list, tree, graph, hash table — each tuned for a particular job. CBSE Class XII introduces only one: Stack.
8.2 What is a Stack?
8.2.1 Everyday examples of a stack
- 🥞 Pile of plates — add or remove only from the top.
- 📚 Stack of books on a table.
- 🌀 Pringles tube / Smarties tube — chips come out in reverse order of the one you put in.
- ⬅️ Browser back-button — the pages you visited are kept in a stack.
- ↩️ Undo in Word, Photoshop, text editors — the most recent action is reversed first.
- 📞 Function call stack — the last function called returns first.
8.2.2 Visualising a Stack
In the picture above, the plates (10, 20, 30, 40) were pushed in that order. A push(50) would place 50 on top; a pop() would remove and return 40 (the most recent plate).
8.3 Stack Operations — the Core Four
| Operation | Does | Returns |
|---|---|---|
push(item) | Insert item on top of the stack | — |
pop() | Remove and return the top item | The removed item |
peek() (or top()) | Look at the top item without removing it | The top item |
isEmpty() | Test whether the stack has no items | True or False |
size() — that returns the number of items. It is handy but not strictly required by CBSE.
8.4 Stack Overflow and Underflow
| Stack Overflow | Stack Underflow | |
|---|---|---|
| When does it happen | Trying to push when the stack is already full | Trying to pop or peek when the stack is empty |
| In a fixed-size stack | Return an error / raise exception | Return an error / raise exception |
| In Python’s list-based stack | Effectively never (list grows automatically) | Must be prevented by checking isEmpty() first |
list can grow as large as the RAM allows, overflow is rarely a concern in practice. Underflow, however, is a real bug — always check isEmpty() before pop or peek.
8.5 Implementing a Stack Using a Python List
A Python list already supports the two operations we need — append() (adds at the end) and pop() (removes from the end). We treat the end of the list as the top of the stack.
| Stack operation | List method |
|---|---|
push(item) | stack.append(item) |
pop() | stack.pop() |
peek() | stack[-1] |
isEmpty() | len(stack) == 0 |
size() | len(stack) |
8.5.1 Using a list directly
8.6 Stack as Functions (CBSE format)
CBSE expects you to write the stack operations as named functions. The stack itself is a normal list that you pass to each function.
8.6.1 Using the functions
8.6.2 Stack as a class (optional, for reference)
8.7 Applications of Stacks
Stacks appear in many places in computer science:
- Reversing a sequence (string, list, number).
- Checking balanced parentheses — compilers use this on every program.
- Converting infix expressions (like
a + b * c) to postfix. - The browser’s Back button and the Forward button (two stacks).
- Undo / Redo in editors.
- Python’s own function-call mechanism — every call pushes a frame.
- Depth-First Search (DFS) in trees and graphs.
8.8 CBSE-style Worked Programs
8.8.1 Menu-driven program for stack operations
8.8.2 Reverse a string using a stack
8.8.3 Check if a string of parentheses is balanced
This is the classic stack problem: for every opening bracket we push; for every closing bracket we pop and check whether the pair matches.
8.8.4 Push every even number from a list into a stack, then display
8.8.5 Push every word longer than N letters into a stack
8.8.6 Reverse a list using a stack
8.8.7 Convert a decimal number to binary using a stack
8.8.8 Undo-simulator for a simple text editor
8.9 Common Mistakes to Avoid
| # | Mistake | Fix |
|---|---|---|
| 1 | Calling pop() on an empty stack | Always check isEmpty() first |
| 2 | Treating the start of the list as the top | Use append() / pop() at the end — it is O(1); inserting at index 0 is slow |
| 3 | Forgetting that pop returns the removed item | Store it: x = stack.pop() |
| 4 | Using stack[0] instead of stack[-1] for peek | Top = last element = [-1] |
| 5 | Displaying the stack bottom-to-top in the exam | Convention is top first — use reversed(stack) |
| 6 | Not resetting the stack between test cases | Create a fresh stack = [] for each |
Quick-revision summary
- A stack is a linear, LIFO data structure — insertion and deletion happen only at the top.
- Four core operations:
push,pop,peek,isEmpty. - In Python, a plain
listis a stack:append()=push,pop()=pop,[-1]=peek,len(s)==0=isEmpty. - CBSE style: write the four operations as named functions that take the list as a parameter.
- Overflow is unlikely with a Python list; underflow must be prevented by checking
isEmpty()first. - Classic applications: reversing sequences, checking balanced parentheses, decimal-to-binary conversion, undo operations, browser back button.
Networks