codeintuition-logo

Exploring a possible solution


Now that we know that some problems can only be solved by processing data items in a LIFO order, we can look at a data structure that inherently maintains this order by design. A stack is a linear container that allows adding and removing items at only one end. This restriction on adding and removing items only from one end ensures that what goes last in the stack comes out first (LIFO).

Stack of plates

A real-life example is a stack of plates. Only the plate at the top can be removed. To remove a plate from the bottom, we need to remove all the plates above it. Similarly, any new plate can only be added at the top of this stack of plates.

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