What is the difference between LIFO and FIFO?

LIFO and FIFO are two techniques of maintaining data in a data structure. They also determine the sequence of entry/exit of data into a data structure. Let’s look at both of the techniques.

LIFO

LIFO stands for Last-in, First-out. In the LIFO technique, as the name suggests, the data which is entered in last will get to exit first.

In data structures, the stack data structure uses the LIFO technique for maintaining the data. Let’s see the graphical representation of the stack data structure.

If we push an element into the stack and pop an element, the data structure looks like this.

You can see that the point of entry and exit of the data is the same. As a result, the data which was entered last will be pulled out first.

You can also notice stack data structure and the LIFO principle in real life. A stack of dish plates is a real-world example of stack data structure. The new plate is to be placed at the top, and eventually will be picked first, compared to the plates underneath it.

FIFO

FIFO stands for First-in, First-out. In the FIFO technique, as the name suggests, the data entered first will also exit first.

In data structures, the queue data structure uses the FIFO technique for maintaining the data. Let’s see the graphical representation of the queue data structure.

Now, if we enqueue and dequeue an element, it looks like this.

You can see that the point of entry is on one side, and the exit point is on another. So, the data that gets inside first will also eventually come out first.

You can also notice the queue data structure and the FIFO principle in real life. One example is a queue of customers to a counter. The new customers join the queue at the end, while the people at the front place their order at the counter and leave the queue first.

Copyright ©2024 Educative, Inc. All rights reserved