Data Structures - Stacks & Queues in Python Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- Data Structure Classification: Linear (elements form a sequential sequence: List, Stack, Queue) vs Non-Linear (elements organized hierarchically: Trees, Graphs).
- Stack Definition: A linear data structure that follows the LIFO (Last In, First Out) or FILO (First In, Last Out) principle. All insertions and deletions take place at a single end called TOP.
- Stack Operations: 1) PUSH (inserting element at top), 2) POP (deleting element from top), 3) PEEK / INSPECT (viewing top element without removing it), 4) isEmpty (testing if stack has zero elements).
- Stack Exceptional Conditions: Overflow (attempting to push into a full stack of fixed capacity) and Underflow (attempting to pop from an empty stack).
- Queue Definition: A linear data structure that follows the FIFO (First In, First Out) principle. Insertions happen at REAR (Enqueue), deletions happen at FRONT (Dequeue).
Chapter Formulas & Equations (10 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | Stack Implementation via Python List Mapping |
|
Python list amortized O(1) append and pop operations at the end make it the ideal implementation for a stack. |
| 2 | Stack Underflow Condition |
|
Must be guarded by checking `if len(s) == 0: print('Underflow')` before calling pop(). |
| 3 | Stack Overflow Condition (Bounded Stack) |
|
In Python dynamic lists, memory is dynamically resized, but bounded stacks enforce capacity limit. |
| 4 | Stack PEEK / Top Element Inspection |
|
Accesses top element in O(1) time without mutating stack structure. |
| 5 | Stack Display from Top to Bottom |
|
Traverses list in reverse to show LIFO order visually. |
| 6 | Queue Enqueue & Dequeue Mapping |
|
In standard lists, pop(0) takes O(n) time; collections.deque provides O(1) popleft(). |
| 7 | Applications of Stack |
|
Key practical use cases in computer science systems. |
| 8 | Infix to Postfix Transformation Rule |
|
Standard Shunting-Yard algorithm using stack. |
| 9 | Postfix Expression Evaluation Rule |
|
Operand popped second is left operand; operand popped first is right operand. |
| 10 | Time Complexity of Stack Operations |
|
All core stack operations take instantaneous constant time. |
Step-by-Step Worked Board Exam Examples
Example 1: Write a complete Python program defining functions `Push(Stk, item)` and `Pop(Stk)` to implement a Stack of employee records `[emp_id, emp_name, salary]`.
1. Define `Push(Stk, item)`: Append item to Stk list.
2. Define `Pop(Stk)`: Check if stack is empty; if so, print 'Underflow' and return None; otherwise, return `Stk.pop()`.
3. Define `Display(Stk)`: Traverse backwards from len(Stk)-1 to 0.
Example 2: Evaluate the following postfix expression using a stack: `25, 5, /, 4, *, 10, -` (show stack status after each operation).
1. Scan '25': Push 25. Stack: [25]
2. Scan '5': Push 5. Stack: [25, 5]
3. Scan '/': Pop 5, Pop 25. Compute 25 / 5 = 5. Push 5. Stack: [5]
4. Scan '4': Push 4. Stack: [5, 4]
5. Scan '*': Pop 4, Pop 5. Compute 5 * 4 = 20. Push 20. Stack: [20]
6. Scan '10': Push 10. Stack: [20, 10]
7. Scan '-': Pop 10, Pop 20. Compute 20 - 10 = 10. Push 10. Stack: [10]
8. End of expression: Final result on top of stack is 10.
Common Mistakes to Avoid in Exam
- Calling `pop()` on an empty stack: Causes `IndexError: pop from empty list`. Always test `if len(stack) == 0` first.
- Reversing operand order in postfix evaluation: For operator `-`, if pop1 is 10 and pop2 is 20, the calculation must be `pop2 - pop1 = 20 - 10 = 10`, NOT `10 - 20`.
- Displaying a stack from index 0 to len-1: A stack must always be visualized and printed from TOP to BOTTOM (index -1 down to 0).
- Confusing LIFO (Stack) with FIFO (Queue): Stack is Last-In-First-Out; Queue is First-In-First-Out.
Target Practice Questions
Q1. If elements A, B, C, D are pushed onto a stack in that order, what is the order of elements removed?
Correct Answer: D, C, B, A (Reverse order due to LIFO).
Q2. Write a one-line Python condition to check if a stack `S` is empty.
Correct Answer: `len(S) == 0` or `not S`
Q3. Give two real-world computer science applications of Stacks.
Correct Answer: 1) Web browser Back and Forward navigation history. 2) Function call stack and recursion management in program execution.
Frequently Asked Questions
Q. What is a Stack and what principle does it follow?
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle, where insertion (push) and deletion (pop) are performed at only one designated end, known as the TOP.
Q. What is Stack Underflow?
Stack Underflow is an error condition that occurs when a program attempts to pop or remove an item from a stack that contains zero elements (an empty stack).
Q. Why are Python lists ideal for implementing Stacks?
Because Python lists implement dynamic arrays where appending an element to the end (`append()`) and removing the last element (`pop()`) both execute in O(1) amortized constant time.
Q. What is the PEEK operation?
PEEK (or Top) allows inspecting the value of the topmost element of the stack without removing or modifying it.
Q. How does a Queue differ from a Stack?
A Queue operates on the FIFO (First In, First Out) principle with two distinct ends: insertions happen at the REAR end, while deletions occur at the FRONT end.