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.

How it works
The drum model:
- A drum is taken and a spring is placed inside it.
- All plates are kept on the spring, one above the other.
- When the top plate is pulled out, the spring pushes the remaining plates up, so the next plate comes to the top.
- 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.
- 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:
- Recursion — the most popular application; the stack is used to implement recursion, and recursion changed the way we think about programming.
- Infix to postfix conversion and evaluation of postfix expressions.
- Parsing — all parsers use the stack; subclasses include matching parentheses, matching tags in HTML, and in general matching any items (parentheses, if–else, etc.).
- 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.
- 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. - 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 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 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 , dequeue amortized ), 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.