What is the LIFO principle in a stack?
The LIFO (Last In, First Out) principle means that the last element added is removed first.
That is:
- The element that enters the stack last ends up on top.
- When removing an element (the
popoperation), it is exactly this top element that is removed. - The remaining elements stay in the stack and wait their turn.
Example:
If you add the elements 1, 2, 3 to the stack (in that order), they come out in reverse order when removed: 3, 2, 1.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.