Stacks

Stack Operations in Data Structures

A guide to stack operations in data structures: push, pop, peek, and isEmpty, how each works, their time complexity, and overflow and underflow.

On this page
  1. What a stack does
  2. The core stack operations
  3. How push and pop work on an array
  4. Stack underflow and overflow
  5. Time complexity of stack operations
  6. Array-based and linked-list stacks
  7. Where stacks are used
  8. Summary

A stack is one of the simplest data structures, and its behavior comes down to one rule: the item added most recently is the first one removed. Stack operations are the actions that let a program add an item, remove one, and look at the item on top. This guide covers the core operations, how they work on an array-based stack, what they cost, and the edge cases, such as popping from an empty stack.

What a stack does

A stack stores items in order and only lets you work at one end, called the top. This is often described as LIFO, for last in, first out. A pile of plates works as a picture of the idea: you place a plate on top, and you take a plate off the top. You do not pull a plate out of the middle.

Because all the action happens at one end, a stack is easier to reason about than a list where items can be inserted or removed anywhere.

The core stack operations

Most descriptions of a stack include these operations:

  • Push adds an item to the top.
  • Pop removes the item at the top and returns it.
  • Peek returns the item at the top without removing it. Some courses call this operation top.
  • isEmpty reports whether the stack has no items.
  • isFull reports whether a fixed-size stack has no room left. This applies to stacks built on a fixed-size array. A linked-list stack has no fixed limit, so it does not need this check.

Names vary between textbooks and languages, but the behavior is the same.

How push and pop work on an array

A common way to build a stack is with an array and an integer that tracks the top position. The steps look like this in pseudocode:

push(x):
    if the stack is full, report overflow
    top = top + 1
    stack[top] = x

pop():
    if the stack is empty, report underflow
    x = stack[top]
    top = top - 1
    return x

This sketch assumes top starts at -1, which marks an empty stack. That starting value is a convention, and the reasons for it are covered in the discussion of why top = -1 in stack.

Suppose you push the letters A, then B. After the first push, the top is at position 0 and holds A. After the second, the top is at position 1 and holds B. A pop returns B, and the top moves back to position 0, so A is now on top.

Stack underflow and overflow

Two error cases come up with almost every stack:

  • Underflow happens when you pop or peek at an empty stack. There is no item to return. Many languages raise an error or exception in this case, and a hand-written version should check for it first.
  • Overflow happens when you push onto a full fixed-size stack. Some implementations avoid this by resizing the underlying storage when it fills up, though memory limits still apply.

These errors are different from the call stack that runs function calls, even though that memory is also last in, first out. The memory side of this is explained in difference between stack and heap memory.

Time complexity of stack operations

In a typical implementation, push, pop, and peek each take constant time, written O(1). The cost of one operation does not grow with the number of items in the stack. This holds for an array-based stack with a fixed size, and for a linked-list stack that adds and removes nodes at the head.

A stack built on a dynamic array, one that grows when it fills, usually has O(1) push on average. An occasional resize costs more, but spread over many pushes the average stays constant. Computer scientists call this amortized O(1). For background on how these labels work, see big o for data structures.

Array-based and linked-list stacks

Both approaches give the same behavior, and they differ in storage:

  • An array-based stack keeps items in a contiguous block and uses an index for the top. It is simple and fast, but a fixed-size version can overflow.
  • A linked-list stack stores each item in a node. The head of the list is the top. It has no fixed size limit, but each push creates a new node in memory.

A program that calls push and pop does not need to know which version is in use, though the costs and limits can differ.

Where stacks are used

Stacks appear in function calls, expression evaluation, undo features, and many other places. Common uses include keeping track of nested function calls, checking that brackets are matched, and handling undo history. The full list is in what are the 10 applications of stack.

Summary

  • A stack follows last in, first out order.
  • The core operations are push, pop, peek, and isEmpty, with isFull for fixed-size stacks.
  • Popping or peeking an empty stack is underflow, and pushing onto a full fixed-size stack is overflow.
  • Push, pop, and peek usually take constant time.
  • Stacks can be built on an array or a linked list, and both fit the same behavior.

Keep exploring

Guides from other topics

All topics →

AVL Trees in Data Structures

An AVL tree is a self-balancing binary search tree. Learn how balance factors and rotations keep search, insert, and delete at O(log n) in the worst case.

AVL Trees 4 min read