PAVANXDCL • DSA FOUNDATION

Master Stack

Understand Stack, LIFO, push, pop, peek, implementation, applications, complexity and important interview patterns.

01 — FOUNDATION

What is a Stack?

A Stack is a linear data structure where insertion and deletion happen at one end called the top.

↥

LIFO

Stack follows Last In, First Out. The last element added is the first element removed.

▣

One Open End

Push, pop and peek are performed at the top of the Stack.

⚡

Simple Operations

The core operations are push, pop, peek and checking whether the Stack is empty.

02 — CORE IDEA

LIFO — Last In, First Out

Like a pile of plates: add to the top and remove from the top.

Push 10Add 10
→
Push 20Add 20 above 10
→
Pop20 is removed first
10
TOP20
20 was inserted last, so it is removed first. This is LIFO.
03 — OPERATIONS

Important Operations

push

Add an element to the top.

pop

Remove the top element.

peek / top

Read the top element without removing it.

isEmpty

Check whether the Stack is empty.

04 — IMPLEMENTATION

How to Implement a Stack?

A Stack can be implemented using an array or a linked list.

ArrayStore elements and track top
→
TopPoints to the current top
→
Linked ListUse the head as top
06 — COMPLEXITY

Time Complexity

OperationComplexityReason
PushO(1)Add at top
PopO(1)Remove from top
Peek / TopO(1)Direct access to top
isEmptyO(1)Check current state
07 — EXAMPLE

Push and Pop Step by Step

[10]Push 10
→
[10, 20]Push 20
→
[10]Pop removes 20
08 — VIDEO LEARNING

Watch Videos

Learn Stack concepts through visual explanations.

▶
BEGINNER

Stack Basics

Understand Stack and LIFO.

▶
[LeetCode]

Valid Parentheses

Learn the core operations.