Stack Visualizer
Peek Operation
Returns the topmost element from the stack without removing it.
Example: Peeking at a stack
Example: Peeking at a stack
- Current stack: [7, 3, 5]
- Peek → returns 7: [7, 3, 5] (stack remains unchanged)
- After pop: [3, 5]
- Peek → returns 3: [3, 5]
- Time Complexity: O(1)
- Space Complexity: O(1)
- Also known as top operation in some implementations
The peek operation is useful when you need to inspect the top element before deciding whether to pop it or push another element onto the stack.
Visualize Push, Pop, and Peek operations
Stack Visualization
Stack is empty
Stack is empty
Top
Current top
Peek
Viewing
Elements
Stack items