Skip to content
Home

Stack (data structure)

An abstract data type that stores elements in last-in, first-out order. Explains operations, implementations, history, use cases, and common variants with practical examples.

Overview

A stack is an abstract data type and a fundamental data structure used to store a collection of elements with restricted access. Elements are added and removed only at one end, commonly called the top. This behavior is often summarized as last-in, first-out (LIFO): the most recently added item is the first to be removed. A familiar everyday analogy is a face-down deck of playing cards: you can take or add only the top card, but you cannot directly reach cards beneath it without removing those above them. For a brief conceptual grounding within computer science, stacks are one of the simplest and most widely used structures.

Image gallery

6 Images

Core operations and properties

Typical operations provided by a stack are few and well defined:

  • push(x) — place element x on the top of the stack;
  • pop() — remove and return the element at the top;
  • peek() / top() — return the top element without removing it;
  • isEmpty() — check whether the stack contains no elements.

Because interaction is limited to the top, stacks typically have simple and efficient time complexity: most operations run in constant time O(1) on common implementations. Memory usage depends on the chosen backing structure and whether the stack is bounded or unbounded.

Implementations and variants

Stacks can be implemented several ways depending on performance and memory constraints. Common approaches include using a contiguous array (with an index for the top) or a linked list (where the head represents the top). Language runtimes and standard libraries often provide stack-like containers or wrappers around dynamic arrays. Variants include:

  • Bounded stacks — fixed maximum size, requiring overflow checks;
  • Persistent/immutable stacks — operations create new versions rather than mutating existing ones, useful in functional programming;
  • Double-ended stacks or deques — generalize access to both ends but can be used as stacks when access is restricted to one end.

History and development

The concept of stack-like storage predates modern computing; early mechanical and paper-based processes used LIFO principles. In computing, stacks became explicit in programming language runtimes and machine architecture: the program call stack manages function calls, local variables, and return addresses, enabling nested subroutines and recursion. Compiler design, expression evaluation, and virtual machine implementations formalized stack usage as an essential tool.

Applications and examples

Stacks appear across many areas of software and theory. Common uses include:

  • Function call management and control flow in most programming languages.
  • Expression evaluation and syntax parsing (infix-to-postfix conversion and postfix evaluation).
  • Backtracking algorithms (e.g., depth-first search), undo mechanisms in editors, and delimiter matching (parentheses checking).
  • Implementation of language interpreters and virtual machines, where stacks hold intermediate results.

To visualize, consider parsing arithmetic: a stack can hold operators while operands are output or computed, allowing correct handling of precedence and associativity. Educational explanations often use a deck of cards as an analog; see the simple analogy at playing cards.

Distinctions and notable facts

Stacks differ from queues, which follow first-in, first-out (FIFO) ordering. While both are sequential containers, their access patterns lead to different algorithmic uses. The call stack is a special, implicit stack that is central to program execution, but many algorithms use explicit stacks to replace recursion or manage state. Because stacks restrict access, they are simpler to reason about and often allow cleaner, more efficient implementations for problems that naturally follow LIFO semantics.

Questions and answers

Q: What is a stack in computer science?

A: A stack is a data structure used in computer science.

Q: What is an example of a stack?

A: An example of a stack is a deck of playing cards that is face down.

Q: How do we access the cards in a stack?

A: We can only easily access the card that is on top of the stack.

Q: What are the two things we can do to the top card of a stack?

A: We can peek at it, but leave it on the stack, or we can pop it off.

Q: What happens when we pop off the top object of a stack?

A: When we pop off the top object, we are taking it off the stack.

Q: What do we call a collection that uses a LIFO structure?

A: A collection that uses a LIFO structure is a last-in-first-out (LIFO) collection.

Q: What is the first thing that gets pulled off a stack?

A: The first thing that gets pulled off a stack is the last thing that was added (pushed) onto it.

Related articles

Author

AlegsaOnline.com Stack (data structure)

URL: https://en.alegsaonline.com/art/93201

Share