Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Queue
  • Practice Queue
  • MCQs on Queue
  • Queue Tutorial
  • Operations
  • Applications
  • Implementation
  • Stack vs Queue
  • Types of Queue
  • Circular Queue
  • Deque
  • Priority Queue
  • Stack using Queue
  • Advantages & Disadvantages
Open In App
Next Article:
Programming or DSA: Which one should I learn first?
Next article icon

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

Last Updated : 06 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
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)


Next Article
Programming or DSA: Which one should I learn first?

C

code_r
Improve
Article Tags :
  • Data Structures
  • DSA
  • Queue
Practice Tags :
  • Data Structures
  • Queue

Similar Reads

  • 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
    6 min read
  • 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, f
    2 min read
  • Programming or DSA: Which one should I learn first?
    Programming and Data Structures and Algorithms (DSA), both are important to learn because they form the foundation of creating computer programs and solving problems effectively. But deciding where to start in computer science can be tricky, i.e. Should you learn Programming first or jump into Data
    10 min read
  • Print "GeeksforGeeks" in 10 different programming languages
    The most elementary part of learning any computer programming language is the ability to print a desired text on the screen or console. Thus, the task of this article is to guide programmers new to any of the 10 different languages discussed below, i.e. GO, Fortran, Pascal, Scala, Perl, ADA, Ruby, K
    4 min read
  • Types of Issues and Errors in Programming/Coding
    "Where there is code, there will be errors" If you have ever been into programming/coding, you must have definitely come across some errors. It is very important for every programmer to be aware of such errors that occur while coding. Table of Content 1. Syntax errors: 2. Logical errors:3. Runtime e
    13 min read
  • Dynamic Programming (DP) Introduction
    Dynamic Programming is a commonly used algorithmic technique used to optimize recursive solutions when same subproblems are called again. The core idea behind DP is to store solutions to subproblems so that each is solved only once. To solve DP problems, we first write a recursive solution in a way
    15+ min read
  • Basics of Combinatorics for Competitive Programming
    Welcome to the world of competitive programming, where we'll explore the ABCs of Combinatorics - a fancy term for counting and arranging things. Think of it like solving puzzles with numbers and patterns. In this article, we're diving into the Basics of Combinatorics that every competitive programme
    10 min read
  • How to become a master in competitive programming?
    There are many people for whom programming is like a haunted dream. Programming is nothing but an art of talking with machines and telling them what to do, when to do, and why to do. Most of the students hear this word in high school. For many of them programming starts with ‘C’ and ends at ‘C’. The
    4 min read
  • Competitive Programming - A Complete Guide
    Competitive Programming is a mental sport that enables you to code a given problem under provided constraints. The purpose of this article is to guide every individual possessing a desire to excel in this sport. This article provides a detailed syllabus for Competitive Programming designed by indust
    8 min read
  • Why is programming important for first year or school students?
    Although computer programming was once seen as a skill reserved for geeks and computer nerds, it’s now regarded as an essential ability for 21st century learners and is becoming a key component of many curriculums, even in primary schools. And as it is becoming essential to learning programming basi
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences