codeintuition-logo

Popping an item from the top of the stack


The pop operation removes the top item from the stack and returns its value. After removing the item from the top, the internal topIndex is also updated so that the second last item now becomes the new top of the stack. The algorithm to remove the top of the stack is quite simple. Let us look at the possible cases we need to consider.

1. Stack is empty

We return -1 here to indicate this is an invalid operation, as no item is at the top of the stack.

Liking the course? Check our discounted plans to continue learning.