FIFO (First-In-First-Out) approach in Programming
Last Updated :
06 Dec, 2022
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:
- Data Structures:
- Certain data structures like Queue and other variants of Queue uses FIFO approach for processing data.
- 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.
- 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++
#include<bits/stdc++.h>
using namespace std;
void print_queue(queue< int > q)
{
while (!q.empty())
{
cout << q.front() << " " ;
q.pop();
}
cout << endl;
}
int main()
{
queue< int > q ;
for ( int i = 0; i < 5; i++)
q.push(i);
cout << "Elements of queue-" ;
print_queue(q);
int removedele = q.front();
q.pop();
cout << "removed element-" << removedele << endl;
print_queue(q);
int head = q.front();
cout << "head of queue-" << head << endl;
int size = q.size();
cout << "Size of queue-" << size;
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();
for ( int i = 0 ; i < 5 ; i++)
q.add(i);
System.out.println( "Elements of queue-" + q);
int removedele = q.remove();
System.out.println( "removed element-" + removedele);
System.out.println(q);
int head = q.peek();
System.out.println( "head of queue-" + head);
int size = q.size();
System.out.println( "Size of queue-" + size);
}
}
|
Python3
q = []
for i in range ( 5 ):
q.append(i)
print ( "Elements of queue-" , q)
removedele = q.pop( 0 )
print ( "removed element-" , removedele)
print (q)
head = q[ 0 ]
print ( "head of queue-" , head)
size = len (q)
print ( "Size of queue-" , size)
|
C#
using System;
using System.Collections.Generic;
public class QueueExample
{
public static void Main(String[] args)
{
Queue< int > q = new Queue< int >();
for ( int i = 0; i < 5; i++)
q.Enqueue(i);
Console.Write( "Elements of queue-" );
foreach ( int s in q)
Console.Write(s + " " );
int removedele = q.Dequeue();
Console.Write( "\nremoved element-" + removedele + "\n" );
foreach ( int s in q)
Console.Write(s + " " );
int head = q.Peek();
Console.Write( "\nhead of queue-" + head);
int size = q.Count;
Console.WriteLine( "\nSize of queue-" + size);
}
}
|
Javascript
<script>
let q = [];
for (let i = 0; i < 5; i++)
q.push(i);
document.write( "Elements of queue-[" + q.join( ", " )+ "]<br>" );
let removedele = q.shift();
document.write( "removed element-" + removedele+ "<br>" );
document.write( "[" +q.join( ", " )+ "]<br>" );
let head = q[0];
document.write( "head of queue-" + head+ "<br>" );
let size = q.length;
document.write( "Size of queue-" + size+ "<br>" );
</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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...