Stacks

What Are the 10 Applications of a Stack?

Ten common uses of a stack in programming and computing, from function calls and expression evaluation to undo history and browser back buttons.

On this page
  1. Why stacks fit so many problems
  2. Ten common applications
  3. What the list has in common
  4. Summary

A stack is useful wherever the most recent item needs to come back first. Many programs and systems rely on that pattern. The list below covers ten common applications. Sources do not agree on one official list, so treat these as typical examples rather than a fixed set. The core operations behind them are covered in stack operations in data structure.

Why stacks fit so many problems

A stack remembers items in the order they arrived and hands back the latest one first. That suits work that is nested, such as a function calling another function, or work that can be reversed one step at a time, such as an edit that can be undone.

Ten common applications

1. Function calls and recursion

When one function calls another, the program saves where to return to and the local values of the caller. Runtimes usually track this on a call stack, which follows last-in, first-out order. Recursion adds more frames to the same stack. If the recursion goes too deep, the program can run out of stack space and crash with a stack overflow. A closely related topic is what is a recursive function.

2. Expression evaluation

Compilers and calculators often use stacks to evaluate arithmetic. An expression written in infix form, such as 3 + 4 * 2, can be converted to postfix form, which is 3 4 2 * +. The postfix form is then evaluated with a stack. Pushing 3, 4, and 2 gives the stack [3, 4, 2]. The multiplication pops 2 and 4 and pushes 8. The addition pops 8 and 3 and pushes 11. Postfix does not need parentheses or precedence rules during evaluation, which is one reason it is used.

3. Checking balanced brackets

To check that parentheses, square brackets, and curly braces are matched, push each opening bracket. When a closing bracket appears, pop the top item and check that it is the matching opening bracket. If every check passes and the stack is empty at the end, the brackets are balanced. The string ({[]}) passes this check, while ([)] fails because the brackets close in the wrong order.

4. Undo and redo

Text editors and drawing tools often keep a stack of past actions. Undo pops the most recent action and reverses it. Many programs also keep a second stack for redo, which holds the actions that were undone. This is a simplified model, since real programs can track history in more detailed ways.

Depth-first search explores as far as it can along one path before returning to try another. An iterative version keeps the nodes still to be explored on a stack. Backtracking puzzles, such as finding a way through a maze or placing queens on a chessboard, follow the same pattern. The program moves forward, and when it reaches a dead end, it pops back to the last choice and tries the next option.

6. Browser back and forward history

A simple model of browser history uses two stacks. One holds the pages you can go back to, and the other holds the pages you can go forward to. Going back pops a page from the back stack and pushes the current page onto the forward stack. Real browsers handle history with more detail, but the stack model explains the basic behavior.

7. Reversing a sequence

If you push every item of a sequence and then pop them all, the order comes out reversed. Pushing the letters of “stack” and popping them gives “kcats”. The same idea works for any list of items where the reversed order is needed.

8. Converting numbers between bases

To convert a decimal number to binary by repeated division by 2, you collect the remainders. They come out in reverse order, and reading them backward gives the binary digits. A stack handles that reversal naturally. For example, 13 in decimal is 1101 in binary. Dividing 13 by 2 repeatedly gives remainders 1, 0, 1, 1 in that order. Popping them gives 1, 1, 0, 1.

9. Operand stacks in virtual machines

Some virtual machines and stack-based languages hold values on an operand stack during calculations. The Java Virtual Machine, for example, uses an operand stack inside each method’s execution frame. Forth and PostScript are also built around stacks.

10. Parsing and syntax checking

Compilers and parsers often track nesting, such as blocks of code or opening and closing tags in markup, with a stack. An opening tag is pushed. When a closing tag appears, the parser checks it against the top of the stack. A mismatch signals a syntax error. The memory side of the call stack is covered in difference between stack and heap memory.

What the list has in common

Each application relies on the same idea. The most recent step is handled first, and older steps wait underneath until they are needed. That matches nested work, such as function calls and brackets, and reversible work, such as undo and back navigation.

Summary

  • A stack suits problems where the most recent item should be handled first.
  • Common uses include function calls, expression evaluation, bracket checking, undo and redo, and backtracking.
  • Other uses include browser history, reversing sequences, number base conversion, virtual machine operand stacks, and syntax checking.
  • Sources differ on the exact list, so these are typical examples rather than a fixed set.

The complete guide to stacks

Stack Operations in Data Structures
Read the guide

Keep exploring

Guides from other topics

All topics →

Is an Array a Data Structure?

Yes, an array is a data structure. Learn what an array is, how it stores elements in memory, what its operations cost, and how it fits into classifications.

Arrays 4 min read