FIFO stands for First In, First Out. It is a way of processing and retriving the data. In FIFO system the first element is processed or servered first and the element which comes next to first element will be processed next.
Real Life Example:
- In a ticket counter people take their ticket and go in an organized manner
- In the line (Queue) at the ticket counter the first person get's the ticket first.
- The person next to the first person will get the ticket next.
- In this manner, the person who enter the queue last will get ticket at last.
This is known as FIFO System.
First in, First out system of approach is used in :-
- Data Structures:
There are data structures like queue and other variants of queue where we use FIFO approach of processing the data. - Disk Scheduling Algorithms:
Disk controllers use FIFo as a disk scheduling algorithm for ordering dist I/O Requests. - Communications and Networking:
FIFO system is used in communication network bridges, switches and routers in computer networks to hold data packets enroute to their next destination.
Program example foe FIFO implementation in Queue:
// 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);
}
} Output:
Elements of queue-[0, 1, 2, 3, 4]
removed element-0
[1, 2, 3, 4]
head of queue-1
Size of queue-4
Contributed by Shyam Kumar With 💜.
Reach me on
