MCS-208 · Unit 4

Stacks

Section 7 of 7

Block 2 · Unit 4Stacks10 min · Section 7 of 7

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:

  1. Scan the text from left to right; ignore other characters.
  2. On an opening bracket, push it.
  3. On a closing bracket: if the stack is empty, or the popped bracket is not the matching kind, the text is not balanced.
  4. 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

ApplicationWhat the stack holds
Undo in editorsPrevious states or actions; undo pops the last one
Browser “Back” buttonPages 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 sequencePush all items, then pop them in reverse order
Number base conversionRemainders 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 1 · multiple choice

Which expression has balanced brackets?

Show answer

Answer: D. [ ( ) { } ]

Every opener is closed by the same kind in reverse order. ) ( has equal counts but the ) comes before any (; only a stack check catches this.

Question 2 · multiple choice

While checking { [ ( ) ] }, what is the largest number of items on the stack at one time?

Show answer

Answer: C. 3

{, [ and ( are pushed before the first closer arrives. The stack never holds closing brackets.

Question 3 · multiple choice

What does the call stack hold for each active function call?

Show answer

Answer: B. An activation record with the return address, parameters and local variables

The frame stores what that particular call needs. The code and global variables are stored elsewhere and are shared by all calls.

Question 4 · multiple choice

What does this program print?

C · q4-reverse.c
#include <stdio.h>
#include <string.h>

int main(void)
{
    const char *word = "STACK";
    char st[10];
    int top = -1;
    for (size_t i = 0; i < strlen(word); i++)
        st[++top] = word[i];
    while (top >= 0)
        putchar(st[top--]);
    putchar('\n');
    return 0;
}
Show answer

Answer: B. KCATS

The letters are pushed S, T, A, C, K and popped in reverse order. Reversing a sequence is one of the simplest stack applications.

Question 5 · multiple choice

A recursive function has no base case and is called once. What usually happens?

Show answer

Answer: D. Each call pushes a new frame until the call stack is exhausted (stack overflow) and the program crashes.

Every call needs a frame, and none ever returns to free one. Compilers usually accept such code; the failure happens at run time.

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.

↑ ↓ move↵ openTab switch type