Computer Science · College · BCA, BTech & MCA
Stacks, Queues & Recursion: LIFO, FIFO & Expression Parsing
Master Stacks (LIFO) and Queues (FIFO), Circular Queues with modular arithmetic, Infix to Postfix conversion, and Call Stack mechanics in recursive execution.
1Stack Data Structure (LIFO Principle)
Stacks, Queues & Recursion: LIFO, FIFO & Expression Parsing
Master Stacks (LIFO) and Queues (FIFO), Circular Queues with modular arithmetic, Infix to Postfix conversion, and Call Stack mechanics in recursive execution.
- 1Subject — Computer Science
- 2Target Audience — BCA, BTech & MCA Students
- 3Topic Core — LIFO, FIFO, Circular Queues & Call Stack
AStackis a linear data structure adhering to theLast-In, First-Out (LIFO)protocol. The element added most recently is the first to be retrieved. All insertions and removals occur exclusively at one designated end termed thetop.
Core Stack Operations and Complexities
| Operation | Description | Array Implementation | Linked List Implementation | Time Complexity |
|---|---|---|---|---|
| push(x) | Insert element x onto top of stack | (check for overflow) | Insert at head () | O(1) |
| pop() | Remove and return the top element | (check for underflow) | Remove from head () | O(1) |
| peek() / top() | Inspect value of top element without removal | O(1) | ||
| isEmpty() | Check if stack has zero elements | O(1) |
push(x)
- Description
- Insert element x onto top of stack
- Array Implementation
- (check for overflow)
- Linked List Implementation
- Insert at head ()
- Time Complexity
- O(1)
pop()
- Description
- Remove and return the top element
- Array Implementation
- (check for underflow)
- Linked List Implementation
- Remove from head ()
- Time Complexity
- O(1)
peek() / top()
- Description
- Inspect value of top element without removal
- Array Implementation
- Linked List Implementation
- Time Complexity
- O(1)
isEmpty()
- Description
- Check if stack has zero elements
- Array Implementation
- Linked List Implementation
- Time Complexity
- O(1)
def is_balanced(s: str) -> bool:
stack = []
mapping = {')': '(', '}': '{', ']': '['}
for ch in s:
if ch in mapping.values():
stack.append(ch) # push opening bracket
elif ch in mapping.keys():
if not stack or stack.pop() != mapping[ch]:
return False
return len(stack) == 0
# Test cases
print(is_balanced("{[()]}")) # True
print(is_balanced("{[(])}")) # FalseTrue False
2Queue Data Structure & Circular Queue Architecture
AQueueoperates on theFirst-In, First-Out (FIFO)discipline. Elements enter at therear() and depart from thefront().
In a standard linear array queue, dequeuing leaves unused vacant memory in the front that cannot be reused without expensive shifting. ACircular Queueovercomes this limitation by wrapping around usingmodular arithmetic: .
Circular Queue State Transitions with Modular Arithmetic
- 1
Initial Empty State
- 2
Enqueue First Element
front = 0, rear = 0, arr[0] = item
- 3
Enqueue Subsequent Elements
rear = (rear + 1) % N, arr[rear] = item
- 4
Full Condition Check
(rear + 1) % N == front (no vacant slot left)
- 5
Dequeue Element
item = arr[front]; if front == rear reset to -1 else front = (front + 1) % N
3Infix to Postfix Conversion & Call Stack in Recursion
Computers cannot efficiently evaluate infix arithmetic expressions directly due to operator precedence and parentheses. UsingDijkstra’s Shunting-yard algorithm, stacks convert human-readable infix notation toPostfix (Reverse Polish Notation), which executes in a single linear pass with no backtracking.
Expression Notations Comparison
| Notation Type | Operator Position | Example Expression | Evaluated By |
|---|---|---|---|
| Infix | Between operands | Human readable, requires precedence rules | |
| Prefix (Polish) | Before operands | Right-to-left scan with stack | |
| Postfix (RPN) | After operands | Left-to-right scan with stack (compilers/calculators) |
Infix
- Operator Position
- Between operands
- Example Expression
- Evaluated By
- Human readable, requires precedence rules
Prefix (Polish)
- Operator Position
- Before operands
- Example Expression
- Evaluated By
- Right-to-left scan with stack
Postfix (RPN)
- Operator Position
- After operands
- Example Expression
- Evaluated By
- Left-to-right scan with stack (compilers/calculators)
Remember
- Stacks follow LIFO (push, pop, peek at top in O(1)); Queues follow FIFO (enqueue at rear, dequeue at front in O(1)).
- Circular Queues utilize modular arithmetic (idx + 1) % N to eliminate memory wastage in array-based FIFO queues.
- Expression parsing (Infix to Postfix) and syntax validation (matching brackets) are primary applications of stacks.
- Recursion relies directly on the system call stack; a missing base case results in StackOverflowError.
Check yourself
Question 1
When converting the infix expression to postfix, what is the resulting postfix expression?Question 2
In an array-based circular queue of size N, what condition indicates that the queue is completely full?