1. Gyanastra
  2. Data Structures
  3. Stacks and Expression Evaluation
  4. Introduction to Stack Data Structure and Its Applications
Free lesson, no account needed 4 min read 18 practice questions after it
Asked in past papers35 times
  • GATE CSE20262025202420232021202020192018201620152014200720062005200420032001199719961994
  • ISRO2017201620152008
  • UGC NET20192018201720162014201320122010200920072006

Data Structures · Stacks and Expression Evaluation

Introduction to Stack Data Structure and Its Applications

Overview

A stack is an old and important data structure in which data is pushed and taken from only one side.
Its behaviour is modelled on a spring-loaded plate drum, and it underpins recursion, expression conversion, parsing, browser history, editor undo/redo, and tree and graph traversals.
The choice of data structure changes the efficiency of an algorithm: searching a sorted array takes less time than searching an unsorted array.

Key concepts

  • Stack: a data structure in which insertion (push) and deletion (pop) happen at only one end, called the top. Data is pushed from one side and taken from the same side; you cannot push from one side and take from the other.
  • Plate-drum origin: glass dining plates were earlier piled one above the other on a table; a slight accidental push would topple and break the pile.
    The fix was a drum with a spring inside, plates kept one above the other on the spring.
  • Traversal: visiting every node (of a tree or graph).
  • Two popular implementations: array-based and linked-list-based.
Data structure diagram: Introduction to Stack Data Structure and Its Applications
Figure. Introduction to Stack Data Structure and Its Applications

How it works

The drum model:

  1. A drum is taken and a spring is placed inside it.
  2. All plates are kept on the spring, one above the other.
  3. When the top plate is pulled out, the spring pushes the remaining plates up, so the next plate comes to the top.
  4. The drum is kept very hot to sterilize the plates, so you cannot put your hands down the drum — only the top plate is reachable.
  5. Hence plates are pushed from the top and taken from the top.

The stack is organized the same way: data goes in and comes out from one side only.

Applications of a stack:

  1. Recursion — the most popular application; the stack is used to implement recursion, and recursion changed the way we think about programming.
  2. Infix to postfix conversion and evaluation of postfix expressions.
  3. Parsing — all parsers use the stack; subclasses include matching parentheses, matching tags in HTML, and in general matching any items (parentheses, if–else, etc.).
  4. Browsers — all visited pages/links are arranged in a stack, which makes the back arrow work: page 1 → link → page 2 → link → page 3 → link → page 4; on page 4, back shows page 3, and back again shows the earlier page.
  5. Editors (undo/redo) — Notepad or any editor maintains a stack of activities: a paste is remembered as "pasted something", typing is remembered with its history.
    Undo goes to the stack and undoes that action; undo again undoes the previous action.
  6. Tree traversals and graph traversals — some traversals use a stack, and some other data structure implementations also take support from the stack.

Revision summary

  • Insertion and deletion happen at only one end, the top — never at opposite ends.
  • The hot drum is why only the top plate is reachable; the spring is why the next plate rises to the top.
  • Memorise the applications: recursion, infix to postfix conversion, parsing (parenthesis, HTML tag, if–else matching), browser back button, editor undo/redo, tree and graph traversals.
  • Traversal means visiting every node.
  • A stack has two popular implementations: array-based and linked-list-based.
  • The data structure chosen changes algorithm efficiency (sorted vs unsorted array search).

Previous-year exam insights

  • A stack is a LIFO (last-in, first-out) structure: push and pop both occur at the top in O(1)O(1) time; underflow (pop on empty) and overflow (push on full array-based stack) are the two error conditions.
    Asked inGATE 2021GATE 2024GATE 2026
  • A queue is FIFO: enqueue at the rear and dequeue from the front; when a stack and a queue are fed the same input sequence, the stack outputs in reverse order and the queue in the same order.
    Asked inGATE 2021GATE 2023GATE 2026
  • To support O(1)O(1) minimum (or maximum) on a stack, store with each element an auxiliary value equal to the minimum (maximum) of all elements from the bottom up to that element; the current answer is the auxiliary value at the top, and it is restored automatically on pop.
    Asked inGATE 2025
  • A single array can implement two stacks efficiently by growing one from index 0 upward and the other from index n-1 downward; overflow occurs only when the two tops meet.
    Asked inGATE 2024
  • A stack can simulate a queue using two stacks (enqueue O(1)O(1), dequeue amortized O(1)O(1)), and a queue can simulate a stack using two queues; a permutation is obtainable as a stack output sequence from input 1..n iff it avoids the pattern 3-1-2.
    Asked inGATE 2016GATE 2024
  • Infix to postfix conversion uses a stack for operators with precedence and associativity rules; postfix evaluation uses a stack of operands, and prefix/postfix expressions need no parentheses.
Pages 1–2 of 2