🔍

Time Complexity of Stack Operations



Stacks follow the Last-In First-Out (LIFO) principle, which influences the time taken to perform operations like push, pop, and peek. In this guide, we’ll explore the time complexities of five core stack operations.

Summary Table

Operation Best Case Average Case Worst Case
Push O(1) O(1) O(1)
Pop O(1) O(1) O(1)
Peek O(1) O(1) O(1)
isEmpty O(1) O(1) O(1)
Traversal O(N) O(N) O(N)

1. Push

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

2. Pop

Pop removes the top element from the stack.

3. Peek

Peek returns the top element without removing it.

4. isEmpty

isEmpty checks whether the stack contains any elements.

5. Traversal

Traversal involves visiting every element from top to bottom of the stack.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M