# What Is FIFO and LIFO in Data Structure?

//

Heather Bennett

Data structures play a crucial role in computer science and programming. They are essential tools for organizing and manipulating data efficiently.

One such data structure is the FIFO and LIFO, which stands for First-In-First-Out and Last-In-First-Out, respectively. In this article, we will explore what FIFO and LIFO are, how they work, and their applications in various scenarios.

## What is FIFO?

FIFO (First-In-First-Out) is a principle that suggests that the first item inserted into a data structure will be the first one to be removed. It follows the same logic as a queue in real life – the first person who enters is the first one to leave. This concept is widely used in various programming scenarios where order matters.

### How does FIFO work?

In programming, FIFO can be implemented using various data structures such as arrays or linked lists. The most common implementation is using a queue – an abstract data type that follows the FIFO principle.

Example:
Let’s say we have a queue with four elements: A, B, C, and D. When we enqueue (insert) these elements into the queue, they are added at the end of the queue. So, A comes first, then B, C, and finally D.
When we dequeue (remove) an element from the queue, it always removes the element from the front of the queue. So, A will be removed first followed by B, C, and D.

Visualization:

• Enqueue(A): Queue = [A]
• Enqueue(B): Queue = [A,B]
• Enqueue(C): Queue = [A,B,C]
• Enqueue(D): Queue = [A,B,C,D]
• Dequeue(): Removed element = A, Queue = [B,C,D]
• Dequeue(): Removed element = B, Queue = [C,D]
• Dequeue(): Removed element = C, Queue = [D]
• Dequeue(): Removed element = D, Queue is empty

## What is LIFO?

LIFO (Last-In-First-Out) is another principle used in programming and data structures. As the name suggests, the last item inserted into a data structure will be the first one to be removed. This principle is commonly used in stacks, where items are stacked on top of each other.

### How does LIFO work?

In programming, LIFO can be implemented using various data structures such as arrays or linked lists. The most common implementation is using a stack – an abstract data type that follows the LIFO principle.

Example:
Let’s say we have a stack with four elements: X, Y, Z, and W. When we push (insert) these elements into the stack, they are added on top of each other. So, X comes first at the bottom of the stack and W comes last at the top.

When we pop (remove) an element from the stack, it always removes the element from the top of the stack. So, W will be removed first followed by Z, Y, and X.

Visualization:

• Push(X): Stack = [X]
• Push(Y): Stack = [X,Y]
• Push(Z): Stack = [X,Y,Z]
• Push(W): Stack = [X,Y,Z,W]
• Pop(): Removed element = W, Stack = [X,Y,Z]
• Pop(): Removed element = Z, Stack = [X,Y]
• Pop(): Removed element = Y, Stack = [X]
• Pop(): Removed element = X, Stack is empty

## FIFO vs. LIFO: Applications

FIFO and LIFO have their applications in different scenarios. FIFO is commonly used when order matters and the oldest item needs to be processed first. It finds applications in task scheduling, printing queues, and handling network packets.

On the other hand, LIFO is used when the most recent items are more important or when we need to backtrack or undo actions. It finds applications in function call stacks, expression evaluation (using postfix notation), and browser history.

In conclusion, FIFO and LIFO are essential concepts in data structures. They provide different ways to organize and manipulate data efficiently based on their respective principles.