Open In App

FIFO Principle of Queue

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

FIFO stands for “First In, First Out”. This principle dictates that the first element added to the queue is the first one to be removed. Understanding the FIFO principle is crucial for anyone working in computer science, especially when dealing with queues.

It ensures that the first task or item you add to the queue is the first to be processed or removed. Whether you’re printing documents, managing tasks, or simulating real-world scenarios, FIFO helps maintain order and fairness.

In Queue Data Structure FIFO has two major operations, Let’s break it down:

Enqueue

When you add an element to the end of the queue, it’s called “enqueuing” that element. This is similar to a person joining the back of the line.

Dequeue

When you remove an element from the front of the queue, it’s called “dequeuing” that element. This is akin to the first person in line being served and leaving.

Why FIFO is Important

FIFO is crucial for many real-life and computer-based scenarios:

Simulating Real-Life Scenarios

  • In the real world, queues are everywhere – from standing in line at a grocery store to handling customer service requests. FIFO helps us mimic these real-life situations in computer programs.

Print Queue

  • Imagine multiple documents to be printed. The first document sent to the printer should be the first one to come out. This is where FIFO ensures fairness.

Task Scheduling

  • In an operating system, when many tasks are competing for resources, FIFO helps manage who gets CPU time first, ensuring fairness and efficiency.

Example of FIFO in a Queue

Let’s illustrate FIFO with a simple example:

  1. Start with an empty queue.
  2. We enqueue elements A, B, and C in that order.
    • Queue: A, B, C
  3. Now, let’s dequeue an element. As per FIFO, we remove A, the first one added.
    • Queue: B, C
  4. Next, we enqueue D.
    • Queue: B, C, D
  5. Dequeue again, and B is the one to go.
    • Queue: C, D

So, just like standing in line at your favourite coffee shop, remember that the first in line is the first to be served when you encounter queues in your programming journey.

To learn about Queue follow this link!!


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads