Open In App

How to Implement a FIFO (First-In-First-Out) Queue in Java?

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

FIFO stands for First In, First Out, which is a common way of organizing and manipulating Data Structure. In FIFO, the first element that is added to the data structure is the same first element that is to be removed first.

Queue is a FIFO-based Data Structure. Here, the insert operation is called Enque, and it is added always at the start, and the delete operation is called the Deque and it is the first element that is removed.

Implementation of FIFO-based Queue

In this article, we will be using a dynamic array to implement our queue. Here, the index is pointing towards the head of the queue. As mentioned, that queue should support both enque and deque, so we need a starting index to show the start point.

Program to Implement a FIFO-based Queue in Java

Below is the implemented code in Java:

Java




// Java Program to implement a FIFO-based Queue
import java.util.*;
import java.util.Queue;
  
class MyQueue {
    // Store Elements
    private List<Integer> data;         
    private int p_start;            
    public MyQueue() {
        data = new ArrayList<Integer>();
        p_start = 0;
    }
    public boolean enQueue(int x) {
        data.add(x);
        return true;
    };    
    
      // Delete the item from queue
    public boolean deQueue() {
        if (isEmpty() == true) {
            return false;
        }
        p_start++;
        return true;
    }
    
       // Find the first item from the queue
    public int Front() {
        return data.get(p_start);
    }
    public boolean isEmpty() {
        return p_start >= data.size();
    }     
};
  
// Driver Class
public class Main {
      // Main Function
    public static void main(String[] args) {
        MyQueue q = new MyQueue();
        q.enQueue(5);
        q.enQueue(3);
        
        if (q.isEmpty() == false) {
            System.out.println(q.Front());
        }
        q.deQueue();
        if (q.isEmpty() == false) {
            System.out.println(q.Front());
        }
         q.deQueue();
        if (q.isEmpty() == false) {
            System.out.println(q.Front());
        }
    }
}


Output

5
3

Explanation of the Program:

  • The above Java program implements a basic queue data structure using a list.
  • The MyQueue class includes methods to enque, deque, and checks if the queue is empty, and retrieve the front element of the queue.
  • It uses a list (data) to store the elements and a pointer (p_start) to keep track of the front element.
  • In the Main class, an instance of MyQueue is created, elements are enqueued (enQueue), and then dequeued (deQueue).
  • The front element of the queue is printed after each operation if the queue is not empty.
  • The third attempt to output the front element will not produce any output as the queue becomes empty after the second deque operation.

Complexities of the Program mentioned above:

  • The overall time complexity for the enque, deque here is O(1). It is because the operation involves the constant operation time involvement, like accessing the elements directly.
  • The overall space complexity is O(n), because there is n number of elements stored inside the queue.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads