⬅ Previous Topic
Stack OperationsNext Topic ⮕
Space Complexity of Stack Operations⬅ Previous Topic
Stack OperationsNext Topic ⮕
Space Complexity of Stack OperationsStacks 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.
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) |
Push adds a new element to the top of the stack.
Pop removes the top element from the stack.
Peek returns the top element without removing it.
isEmpty checks whether the stack contains any elements.
Traversal involves visiting every element from top to bottom of the stack.
⬅ Previous Topic
Stack OperationsNext Topic ⮕
Space Complexity of Stack OperationsYou 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.