100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Computer Science

Stack

BeginnerConcept1.9K learners

A stack is a linear data structure that follows Last-In-First-Out (LIFO) ordering: elements are added and removed from the same end, called the top, so the most recently inserted element is the first one removed. Core operations — push,…

Definition

A stack is a linear data structure that follows Last-In-First-Out (LIFO) ordering: elements are added and removed from the same end, called the top, so the most recently inserted element is the first one removed. Core operations — push, pop, and peek — all run in O(1), making stacks fundamental to function call management, expression evaluation, and undo mechanisms.

Overview

A stack restricts access to a single end, unlike a queue which allows access at both front and rear. Push adds an element to the top; pop removes and returns the top element; peek (or top) returns the top element without removing it. Because operations only touch the top, a stack can be implemented efficiently with either a dynamic array (amortized O(1) push/pop) or a singly linked list (true O(1) push/pop at the head). The most important real-world stack is implicit: the call stack, which every program uses to track function calls. Each function call pushes a new stack frame (containing local variables, parameters, and the return address) and returns by popping that frame, which is why deep or unbounded recursion causes a stack overflow. Compilers and interpreters use explicit stacks for parsing expressions, matching balanced brackets/parentheses, and implementing recursive algorithms iteratively. Stacks are the natural structure for depth-first search (DFS) on trees and graphs, either via the implicit call stack in a recursive implementation or an explicit stack in an iterative one. Other classic applications include undo/redo functionality in editors (each action pushed onto a stack, popped on undo), browser back/forward navigation history, backtracking algorithms (maze solving, N-Queens), and evaluating postfix/infix expressions using operator and operand stacks. Because a stack only exposes its top element, searching for an arbitrary value requires popping elements until found — an O(n) operation — making stacks unsuitable when random access or search is a primary requirement.

Key Concepts

  • Last-In-First-Out (LIFO) ordering
  • Core operations: push, pop, peek, all O(1)
  • Implemented via dynamic array or linked list
  • Underlies the call stack for function invocation and recursion
  • Backbone of depth-first search traversal
  • Used for balanced-bracket checking and expression evaluation
  • Powers undo/redo and browser back/forward navigation

Use Cases

Managing function calls and recursion via the call stack
Implementing depth-first search on trees and graphs
Validating balanced parentheses/brackets in code or expressions
Building undo/redo functionality in text and image editors
Evaluating postfix and infix mathematical expressions
Implementing browser back/forward navigation history
Solving backtracking problems like maze traversal or N-Queens

Frequently Asked Questions

From the Blog