DSA Stack

Store and process values using the Last In, First Out principle.

Overview

A stack is a linear data structure in which the most recently added item is removed first. This rule is called LIFO—Last In, First Out. Stacks support browser navigation, undo systems, function calls, expression evaluation, and many common algorithms.

Key concepts

  • Elements are added and removed only at the top
  • Push adds a value
  • Pop removes the newest value
  • Peek reads the newest value without removing it

What is a Stack?

Imagine a stack of books. Each new book is placed on top, and the top book must be removed before any book beneath it. A stack works in exactly the same way.

Output
Top
 
| 40 |
| 30 |
| 20 |
| 10 |

If an element is removed, 40 leaves first because it is currently at the top.

Why do we need a Stack?

Stacks are useful whenever the most recent action or value must be handled first.

  • Undo and redo in text editors
  • Browser back navigation
  • Function-call management
  • Expression evaluation
  • Parentheses matching
  • Navigation history

Basic Stack operations

Push

Push adds a new element to the top of the stack.

Output
Before: [10, 20]
Push 30
After:  [10, 20, 30]  Top

Pop

Pop removes and returns the top element.

Output
Before: [10, 20, 30]  Top
Pop 30
After:  [10, 20]  Top

Peek

Peek returns the top element without removing it. If the stack contains [10, 20], peek returns 20 and leaves the stack unchanged.

Is Empty

This operation returns true when the stack has no elements and false otherwise.

Implementing a Stack in JavaScript

JavaScript arrays provide push and pop methods that operate efficiently at the end of the array.

JavaScript
const stack = [];

stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack); // [10, 20, 30]

stack.pop();
console.log(stack); // [10, 20]

const top = stack[stack.length - 1];
console.log(top); // 20

How a Stack changes

Suppose we push 10, push 20, push 30, pop once, and then push 40.

Output
[]
 Push 10
[10]
 Push 20
[10, 20]
 Push 30
[10, 20, 30]
 Pop
[10, 20]
 Push 40
[10, 20, 40]

Only the top of the stack changes during these operations.

Time complexity of Stack operations

OperationTime complexity
PushO(1)
PopO(1)
PeekO(1)
Is EmptyO(1)

These operations are fast because they do not require shifting other elements.

Real-life examples

Dinner plates follow LIFO: the plate placed on top most recently is taken first. Text editors also keep actions on a stack so Undo can reverse the most recent action before earlier ones.

Advantages of a Stack

  • Simple to understand and implement
  • Constant-time insertion and deletion
  • Efficient for tracking recent actions
  • Natural support for nested operations
  • Widely useful in languages and applications

Limitations of a Stack

  • Only the top element is directly accessible
  • Searching may require examining many items
  • Random access is not supported
  • It is unsuitable when values must be processed in arbitrary order

Where are Stacks used?

  • Browser back and forward history
  • Undo and redo features
  • Function calls
  • Expression evaluation
  • Code syntax checking
  • Parentheses balancing
  • Depth-First Search
  • Backtracking

Tips for beginners

  • Remember the LIFO rule
  • Practice Push and Pop until they feel natural
  • Draw diagrams after each operation
  • Try reversing a string with a stack
  • Compare stacks with FIFO queues

Key takeaways

  • A stack follows Last In, First Out
  • All changes occur at the top
  • The main operations are Push, Pop, Peek, and Is Empty
  • Core stack operations are O(1)
  • Stacks support history, function calls, syntax checking, DFS, and backtracking
  • Stacks do not provide random access
Let's learn with DevBrainBox AI