LIFO stands for Last In First Out. LIFO was developed as an alternative to the base stock method in inventory accounting and meant that the most recently sold items were recorded as sold first.
In technical terms, LIFO means that the element which was stored as the last element will be the first to be removed. It happens so because the new element will be inserted above the existing element and thus the newest element will be available at the top and it will be removed. Thus, the oldest element or the element that was encountered first during this operation will get out last as it will be at the bottom.
Here, a stack of CDs is available. Now, if we remove the topmost CD, we will be able to access the CD just below it. But if we want to access the CD at the bottom of the stack, we will have to remove all the CDs above it. This will be the only way to access the bottom-most CD.
Characteristics of LIFO
- Stack, a linear data structure is implemented using the LIFO principle.
- Elements can be added/ removed from the same end referred to as top.
- A fixed amount of memory is not consumed as memory utilization will vary with each operation.Thus, a fixed size is not required.
The following general operations are carried out on the Stack data structure –
- Push operation: It means inserting an element at the top of the stack.
- Pop operation: It means removing the topmost element from the stack.
- Peek operation: It means returning the topmost element without deleting it from the stack.
- It is easy to implement and understand.
- It is used in many real-world applications like reversing a string, undo-redo operation and in function calls.
- Elements cannot be accessed randomly as LIFO only supports Last In First Out methodology.
- It is a less flexible method.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.