Stack
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
Frequently Asked Questions
From the Blog
Project: Build a Full-Stack To-Do App with React, Node.js and MongoDB
A full-stack to-do app is the perfect first MERN project — it covers every concept you'll use in production: REST APIs, database CRUD operations, JWT authentication, and deploying a frontend and backend separately. Build it once, understand the full stack.
Read More Success StoriesFrom Finance to Full-Stack Developer: An Illustrative 10-Month Journey
This composite illustrative story follows how a chartered accountant used financial modelling skills and systematic self-study to transition into full-stack development, landing a junior developer role in 10 months without a coding bootcamp.
Read More Data ScienceNumPy for Data Science: Arrays and Vectorisation
NumPy is the foundation of Python's scientific computing stack. This guide covers ndarrays, vectorised operations, broadcasting, linear algebra, and why NumPy is 10-100x faster than equivalent Python loops — with practical examples for data science work.
Read More