LIFO - last in first out and FIFO - First in First out
LIFO and FIFO are two methods of storing data in a data structure. They also define the order in which data enters/exits the data structure. Let's take a look at both techniques.
LIFO
LIFO stands for "last in first out". In the LIFO method, as the name suggests, data that is entered last is exits first.In data structures, the stack data structure uses the LIFO method to store data. Let's look at a graphical representation of the stack data structure.
Stock Data Structure LIFO ( last in first out )
If we push an element onto the stack and pop or exit the element, the data structure will look like this.
Stock Data Structure Push / Add and Pop / Remove Process
You can see that the data entry and exit points are the same. As a result, the data that was entered last will be retrieved first.
You may also notice the stack data structure and the LIFO principle in real life. A stack of plates in Parties is a real example of a stack data structure. The new plate must be placed on top and will eventually be selected first compared to the plates below it.
FIFO
FIFO stands for "First in First out". In a FIFO method, as the name suggests, the data entered first will also be the first to exit. In data structures, the queue data structure uses the FIFO method to store data. Let's look at a graphical representation of the queue data structure.
Queue data structure FIFO ( First In First Out )
Now if we
add and remove an element from the queue, it looks like this.
Queue data structure FIFO (First In First Out ) Add and Remove
You can see that the entry point is on one side and the exit point is on the other. So the data that gets in first ends up being the first to go out.You may also notice the queue data structure and the FIFO principle in real life. One example is the line of customers at the counter. New customers join the queue at the end, while the people in the front place their order at the counter and exit the queue first.