Oh My Algorithm
Algorithm Guidecomplexity: O(1) push/pop

Stack

A last-in-first-out (LIFO) structure where the last item in comes out first. Insertion and removal happen only at one end (the top), and it's the backbone of the call stack, undo, bracket matching, and DFS.

01Stack

Starting the stack. A last-in-first-out (LIFO) structure where you push and pop only at one end (top).

push(10) · Place 10 on top.

push(24) · Place 24 on top.

push(37) · Place 37 on top.

peek() · Inspect the top value 37 without removing it.

pop() · Mark the top 37 for removal.

37 is popped. top moves down one to 24.

pop() · This time mark the top 24 for removal.

24 is popped. The last value in comes out first — that's LIFO.

empty
1 / 9

02 Understand It Simply

For Everyone
🔑How It Works

Values go in and come out at the same end, so the last one in is the first one out.

💡In Plain Words

You add and remove only at one end (the top).

The most recently added item comes out first (last-in-first-out, LIFO).

📍Where It's Used
  • Undo (Ctrl+Z)
  • browser back button
  • bracket-pair checking

03 Python Implementation

A clean, readable reference implementation of the core logic of Stack.

core_implementation.py
class Stack:
    def __init__(self):
        self.items = []

    def push(self, x):
        self.items.append(x)

    def pop(self):
        if not self.items:
            raise IndexError("stack is empty")
        return self.items.pop()

    def peek(self):
        return self.items[-1] if self.items else None

    def is_empty(self):
        return len(self.items) == 0

04 Frequently Asked Questions

FAQ
What is Stack?+

A last-in-first-out (LIFO) structure where the last item in comes out first. Insertion and removal happen only at one end (the top), and it's the backbone of the call stack, undo, bracket matching, and DFS.

What is the time complexity of Stack?+

The time complexity of Stack is O(1) push/pop. Follow the step-by-step visualization to see exactly why.

Where is Stack used?+

Undo (Ctrl+Z), browser back button, bracket-pair checking.

What's a simple analogy for Stack?+

Values go in and come out at the same end, so the last one in is the first one out.