Stack Visualizer

Peek Operation

Returns the topmost element from the stack without removing it.

Example: Peeking at a stack
  1. Current stack: [7, 3, 5]
  2. Peek → returns 7: [7, 3, 5] (stack remains unchanged)
  3. After pop: [3, 5]
  4. 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

Explore other operations