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 currentSize is also decremented. The algorithm for removing a value from the top of the stack is quite straightforward. Let's look at the possible cases we need to consider.

1. Stack is empty

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

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