# What Is First In, First Out in Data Structure?

//

Scott Campbell

## What Is First In, First Out in Data Structure?

In computer science, the term “First In, First Out” (FIFO) refers to a data structure principle that determines the order in which elements are accessed or processed. FIFO operates on the basis that the first element added to a collection will be the first one to be removed.

This concept is analogous to a queue in real life, where people line up and are served in the order they arrived.

### Understanding FIFO

The FIFO principle is commonly implemented using a data structure called a queue. A queue is an abstract data type that follows the FIFO rule.

It can be visualized as a line of elements, where new elements are inserted at one end (known as the “rear” or “tail”) and existing elements are removed from the other end (known as the “front” or “head”).

### The Queue Operations

To work with a queue, we typically perform two fundamental operations:

• Enqueue: Adding an element to the rear of the queue.
• Dequeue: Removing an element from the front of the queue.

These operations ensure that elements are processed in their arrival order. When we enqueue an element, it joins others at the rear of the queue.

On the other hand, when we dequeue an element, it is removed from the front of the queue.

### Real-World Examples

FIFO has various real-world applications. Let’s consider some examples:

• Print Queue: When multiple print jobs are sent to a printer, they are typically processed in the order they were received. The first document to be printed is the one that entered the print queue first.
• Supermarket Checkout: In a supermarket, customers join a queue at the checkout counter. The cashier serves customers one by one in the order they arrived.

• Message Queues: In messaging systems or email servers, messages are often placed in a queue until they can be processed or delivered. The messages are then fetched and handled based on their arrival order.

### FIFO Implementation

FIFO can be implemented using various data structures, such as arrays or linked lists. While arrays provide constant-time access to elements, linked lists offer efficient insertion and deletion at both ends of the list.

The specific implementation choice depends on the requirements of the application.

### Conclusion

First In, First Out (FIFO) is a fundamental principle in computer science that ensures elements are processed or accessed in their arrival order. It is commonly implemented using a data structure called a queue.

Understanding FIFO and its applications can help you design efficient algorithms and solve real-world problems effectively.