Home

2 minute read

FIFO vs LIFO in programming

Tony Lea

FIFO vs LIFO in programming

I've recently been doing some research on FIFO (first-in, first-out) and LIFO (last-in, first-out) organization.

I learned this simple concept back in college; however, I still got a little confused every time I recalled this data-structure organization. Because, when something is last, does this mean it is the final one... or is it most recent 🤔

I wanted a great way to remember this concept, so I created a nice analogy that includes people at a bar 🍻.

Before, I jump into the analogy. Let's first clarify the meaning of FIFO and LIFO.

What is FIFO and LIFO

FIFO and LIFO are two different methods of organizing and manipulating a data structure in computing.

If this sounds complicated, it's not at all, and that's why I want to help you visualize these two methods in a fun way.

The Analogy

It's a Friday night, and you're ready to hit up the pub with some friends 🍺.

In this use-case, we'll say that each person who enters the bar cannot pass anyone else. They must stay in a line (represented in the illustration below)

fifo-vs-lifo.jpg

Now, after everyone has had their fair share of beers and they're ready to head home, there are two ways that they can exit by utilizing the FIFO or the LIFO approach.

FIFO (first-in, first-out)

Using the FIFO approach, we say that the first person who entered will be the first person to leave.

fifo.jpg

The FIFO technique may also be referred to as a queue or a line. If you were to visit your favorite restaurant, you might have to wait in line. The person who arrived before you will get their food first and will be the first to leave. Then it will be your turn to get your food and leave.

LIFO (last-in, first-out)

The LIFO approach means that the last person who entered will be the first person to leave.

lifo.jpg

There is no back door to the pub in this scenario, which means that the first person who entered will have to wait until everyone else has finished their beers before leaving.

The LIFO technique may also be referred to as a stack. In this scenario, the first item on the stack will have to wait until every other item has processed before leaving the stack.

It may be helpful to think of this stacker toy.

stacker-toy.jpeg

After adding all the rings to the toy, you cannot take off the bottom ring until you've taken off the rest. This can be looked at as a LIFO type of organization.

Conclusion

There are times when Acronyms and terms can seem so confusing, yet the underlying meaning behind it is so simple. In those cases, it helps to have examples like the ones above. 👆

I hope this helped you clear up any uncertainties you had about LIFO/FIFO techniques 🙌.