Prerequisites – FIFO (First-In-First-Out) approach in Programming, FIFO vs LIFO approach in Programming
LIFO is an abbreviation for last in, first out. It is a method for handling data structures where the first element is processed last and the last element is processed first.
Real life example:
In this example, following things are to be considered:
- There is a bucket which holds balls.
- Different types of balls are entered in the bucket.
- The ball to enter the bucket last, will be taken out first.
- The ball entering the bucket next to last will be taken out after the ball above it (the newer one).
- In this way, the ball entering the bucket first will leave the bucket last.
- Therefore, the Last ball (Blue) to enter the bucket gets removed first and the First ball (Red) to enter the bucket gets removed last.
This is known as Last-In-First-Out approach or LIFO.
Where is LIFO used:
- Data Structures –
Certain data structures like Stacks and other variants of Stacks uses LIFO approach for processing data.
- Extracting latest information –
Sometimes computers use LIFO when data is extracted from an array or data buffer. When it is required to get the most recent information entered, the LIFO approach is used.
Program Examples for LIFO –
Using Stack data structure:
Pop: 4 3 2 1 0 Element on stack top : 4 Element is found at position 3 Element not found
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- FIFO vs LIFO approach in Programming
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Calculate weight of parenthesis based on the given conditions
- Check given string is oddly palindrome or not | Set 2
- Level order traversal in spiral form using stack and multimap
- Find the missing end tag in the given HTML Code
- Unique subsequences of length K with given sum
- Check if a string can be split into even length palindromic substrings
- Maximize removal of adjacent array elements based on their absolute value
- Sum of decomposition values of all suffixes of an Array
- Real-time application of Data Structures
- Maximize sum of topmost elements of S stacks by popping at most N elements
- Reverse the elements only at odd positions in the given Array
- Program to convert Infix notation to Expression Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.