Open In App

FIFO (First-In-First-Out) approach in Programming

Last Updated : 06 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last.

Real-life example:
 

In this example, following things are to be considered: 

  • There is a ticket counter where people come, take tickets and go.
  • People enter a line (queue) to get to the Ticket Counter in an organized manner.
  • The person to enter the queue first, will get the ticket first and leave the queue.
  • The person entering the queue next will get the ticket after the person in front of him
  • In this way, the person entering the queue last will the tickets last
  • Therefore, the First person to enter the queue gets the ticket first and the Last person to enter the queue gets the ticket last.

This is known as First-In-First-Out approach or FIFO.

Where is FIFO used:

  1. Data Structures:
    • Certain data structures like Queue and other variants of Queue uses FIFO approach for processing data. 
  2. Disk scheduling:
    • Disk controllers can use the FIFO as a disk scheduling algorithm to determine the order in which to service disk I/O requests. 
  3. Communications and networking”
    • Communication network bridges, switches and routers used in computer networks use FIFOs to hold data packets en route to their next destination.

Program Examples for FIFO

Program 1: Queue 

C++




// C++ program to demonstrate
// working of FIFO
// using Queue interface in C++
 
#include<bits/stdc++.h>
using namespace std;
 
// print the elements of queue
void print_queue(queue<int> q)
{
    while (!q.empty())
    {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
}
 
// Driver code
int main()
{
    queue<int> q ;
 
    // Adds elements {0, 1, 2, 3, 4} to queue
    for (int i = 0; i < 5; i++)
        q.push(i);
 
    // Display contents of the queue.
    cout << "Elements of queue-";
         
    print_queue(q);
 
    // To remove the head of queue.
    // In this the oldest element '0' will be removed
    int removedele = q.front();
    q.pop();
    cout << "removed element-" << removedele << endl;
 
    print_queue(q);
 
    // To view the head of queue
    int head = q.front();
    cout << "head of queue-" << head << endl;
 
    // Rest all methods of collection interface,
    // Like size and contains can be used with this
    // implementation.
    int size = q.size();
    cout << "Size of queue-" << size;
         
    return 0;
}
 
// This code is contributed by Arnab Kundu


Java




// Java program to demonstrate
// working of FIFO
// using Queue interface in Java
 
import java.util.LinkedList;
import java.util.Queue;
 
public class QueueExample {
    public static void main(String[] args)
    {
        Queue<Integer> q = new LinkedList<>();
 
        // Adds elements {0, 1, 2, 3, 4} to queue
        for (int i = 0; i < 5; i++)
            q.add(i);
 
        // Display contents of the queue.
        System.out.println("Elements of queue-" + q);
 
        // To remove the head of queue.
        // In this the oldest element '0' will be removed
        int removedele = q.remove();
        System.out.println("removed element-" + removedele);
 
        System.out.println(q);
 
        // To view the head of queue
        int head = q.peek();
        System.out.println("head of queue-" + head);
 
        // Rest all methods of collection interface,
        // Like size and contains can be used with this
        // implementation.
        int size = q.size();
        System.out.println("Size of queue-" + size);
    }
}


Python3




# Python program to demonstrate
# working of FIFO
# using Queue interface in Python
 
q = []
 
# Adds elements {0, 1, 2, 3, 4} to queue
for i in range(5):
    q.append(i)
 
# Display contents of the queue.
print("Elements of queue-" , q)
 
# To remove the head of queue.
# In this the oldest element '0' will be removed
removedele = q.pop(0)
print("removed element-" , removedele)
 
print(q)
 
# To view the head of queue
head = q[0]
print("head of queue-" , head)
 
# Rest all methods of collection interface,
# Like size and contains can be used with this
# implementation.
size = len(q)
print("Size of queue-" , size)
 
# This code is contributed by patel2127.


C#




// C# program to demonstrate
// working of FIFO
using System;
using System.Collections.Generic;
 
public class QueueExample
{
    public static void Main(String[] args)
    {
        Queue<int> q = new Queue<int>();
 
        // Adds elements {0, 1, 2, 3, 4} to queue
        for (int i = 0; i < 5; i++)
            q.Enqueue(i);
 
        // Display contents of the queue.
        Console.Write("Elements of queue-");
        foreach(int s in q)
                Console.Write(s + " ");
 
        // To remove the head of queue.
        // In this the oldest element '0' will be removed
        int removedele = q.Dequeue();
        Console.Write("\nremoved element-" + removedele + "\n");
        foreach(int s in q)
                Console.Write(s + " ");
 
        // To view the head of queue
        int head = q.Peek();
        Console.Write("\nhead of queue-" + head);
 
        // Rest all methods of collection interface,
        // Like size and contains can be used with this
        // implementation.
        int size = q.Count;
        Console.WriteLine("\nSize of queue-" + size);
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript




<script>
 
// JavaScript program to demonstrate
// working of FIFO
// using Queue interface in Java
 
let q = [];
// Adds elements {0, 1, 2, 3, 4} to queue
for (let i = 0; i < 5; i++)
    q.push(i);
 
// Display contents of the queue.
document.write("Elements of queue-[" + q.join(", ")+"]<br>");
 
// To remove the head of queue.
// In this the oldest element '0' will be removed
let removedele = q.shift();
document.write("removed element-" + removedele+"<br>");
 
document.write("["+q.join(", ")+"]<br>");
 
// To view the head of queue
let head = q[0];
document.write("head of queue-" + head+"<br>");
 
// Rest all methods of collection interface,
// Like size and contains can be used with this
// implementation.
let size = q.length;
document.write("Size of queue-" + size+"<br>");
 
 
// This code is contributed by avanitrachhadiya2155
 
</script>


Output

Elements of queue-0 1 2 3 4 
removed element-0
1 2 3 4 
head of queue-1
Size of queue-4

Complexities Analysis:

  • Time Complexity: O(N)
  • Space Complexity: O(N)


Similar Reads

FIFO vs LIFO approach in Programming
FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last. Prerequisite - FIFO (First-In-First-Out) approach in Programming Real-life example: LIFO is an abbreviation for Last in, first out is the same as first in, last out (FILO).
2 min read
LIFO (Last-In-First-Out) approach in Programming
Prerequisites - FIFO (First-In-First-Out) approach in Programming, FIFO vs LIFO approach in Programming LIFO is an abbreviation for last in, first out. It is a method for handling data structures where the first element is processed last and the last element is processed first. Real-life example: In this example, following things are to be consider
6 min read
FIFO Push Relabel Algorithm
The push–relabel algorithm (alternatively, pre flow–push algorithm) is an algorithm for computing maximum flows in a flow network. Push-relabel algorithms work in a more localized manner than the Ford Fulkerson method. Rather than examine the entire residual network to find an augmenting path, push-relabel algorithms work on one vertex at a time, l
15+ min read
FIFO Principle of Queue
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
2 min read
Program for Page Replacement Algorithms | Set 2 (FIFO)
Prerequisite : Page Replacement Algorithms In operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newl
10 min read
Longest subsequence with a given OR value : Dynamic Programming Approach
Given an array arr[], the task is to find the longest subsequence with a given OR value M. If there is no such sub-sequence then print 0.Examples: Input: arr[] = {3, 7, 2, 3}, M = 3 Output: 3 {3, 2, 3} is the required subsequence 3 | 2 | 3 = 3Input: arr[] = {2, 2}, M = 3 Output: 0 Approach: A simple solution is to generate all the possible sub-sequ
6 min read
A Better Way To Approach Competitive Programming
This article helps to all those who want to begin with Competitive Programming. The only prerequisite one need is the knowledge of a programming language. Now, Let us find a better approach to Competitive Programming. Please note: One should read the proper Input and Output format because most of the beginners make mistakes of having extra print st
4 min read
Greedy Approach vs Dynamic programming
Greedy approach and Dynamic programming are two different algorithmic approaches that can be used to solve optimization problems. Here are the main differences between these two approaches: Greedy Approach:The greedy approach makes the best choice at each step with the hope of finding a global optimum solution.It selects the locally optimal solutio
2 min read
Queue based approach for first non-repeating character in a stream
Given a stream of characters and we have to find first non repeating character each time a character is inserted to the stream. Examples: Input : a a b c Output : a -1 b b Input : a a c Output : a -1 cRecommended PracticeFirst non-repeating character in a streamTry It! We have already discussed a Doubly linked list based approach in the previous po
5 min read
Competitive Programming vs General Programming
Programming enthusiasts face a crossroads - Competitive Programming or General Programming? This article sheds light on the distinctions between Competitive Programming vs General Programming, offering a quick guide for those navigating the coding landscape. Whether you're aiming for speed and precision in competitions or tackling real-world projec
3 min read
Article Tags :
Practice Tags :