Peek Operation
Returns the topmost element from the stack without removing it.
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)
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.