4.7More Applications: Brackets, Function Calls and Recursion
Covers IGNOU MCS-208 §4.5, 4.6
Stacks appear wherever something must be “undone” or “closed” in reverse order of opening. This page shows three important cases: checking brackets, running function calls (including recursion), and a few everyday uses.
Checking balanced brackets
An expression has balanced brackets when every opening bracket (, [, { has a matching closing bracket of the same kind, and the pairs are properly nested. Algorithm:
Scan the text from left to right; ignore other characters.
On an opening bracket, push it.
On a closing bracket: if the stack is empty, or the popped bracket is not the matching kind, the text is not balanced.
At the end, the text is balanced only if the stack is empty.
Compilers use exactly this check to report “missing }” or “unexpected )”.
Function calls and the call stack
When a function is called, the program must remember where to continue afterwards and must keep the caller’s local variables safe. It does this on the call stack: each call pushes an activation record (stack frame) holding the return address, the parameters and the local variables; when the function returns, its record is popped and execution continues at the saved address.
Because the function called last always returns first, a stack is exactly the right structure. This is also what makes recursion possible: each recursive call gets its own frame, with its own copy of the parameters, so the calls do not overwrite each other.
If recursion goes too deep (for example, a recursive function with no base case), the call stack runs out of space: this is a stack overflow error. Any recursive algorithm can be rewritten as a loop with an explicit stack.
Other uses
Application
What the stack holds
Undo in editors
Previous states or actions; undo pops the last one
Browser “Back” button
Pages visited; back pops the current page
Backtracking (maze solving, depth-first search in Unit 8)
The path taken so far; on a dead end, pop and try another way
Reversing a sequence
Push all items, then pop them in reverse order
Number base conversion
Remainders of repeated division, popped in reverse to form the digits
Key takeaways
Balanced brackets: push openers; each closer must match the popped opener; the stack must end empty.
Each function call pushes an activation record (return address, parameters, locals); return pops it.
Recursion works because every call has its own frame; too deep recursion causes stack overflow.
Undo, back buttons, backtracking and reversal are further stack applications.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write an algorithm that checks whether the brackets (), [] and {} in an expression are balanced, and trace it on { ( a + b ] }.
Show worked answer
Algorithm: create an empty stack. For each character c from left to right: if c is (, [ or {, push it; if c is ), ] or }: if the stack is empty, return “not balanced”; pop o; if o is not the opener matching c, return “not balanced”. Ignore other characters. At the end, return “balanced” if the stack is empty, otherwise “not balanced”. Trace: { push → [ { ]; ( push → [ { ( ]; a, +, b ignored; ] pops (, which does not match ] → not balanced.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/stacks/more-applications-brackets-function-calls-and-recursion/ to read this page.