StackEasy

Peek / Top

Stack Peek / Top Operation

Peek Operation

The Peek (or Top) operation returns the value of the top element of the stack without removing it. It is a read-only operation.

Key Rules

  1. Empty Check: Before reading, verify if the stack has elements (top >= 0). If the stack is empty, it returns an error value or exception.
  2. Access Top: Directly return arr[top]. Unlike pop, the top index pointer is not decremented.

Visually

Peek() returns 30:
  Index [2]: [  30   ] ◄top (Highlighted / Read)
  Index [1]: [  20   ]
  Index [0]: [  10   ]