Open In App

Java Program to Implement the Queue Data Structure

Last Updated : 27 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Queue is the fundamental data structure that follows the First-In-First-Out(FIFO) principle where the element that is inserted first is one that gets removed first. Imagine the queue of the people waiting in the line at the ticket counter: the person who arrives the first gets served first and so on.

Organization of the Queue Data Structure

The queue can be visualized as the line of the elements where elements are inserted at one end(rear) and removed from the other end(front). This structure can resemble the physical queue of the people waiting in line.


Implementation

Queue can be implemented using the arrays or linked lists. In the array-based implementation, we can use the pointers front and rear to keep track of the elements. In the linked list implementation, each node contains the data elements and the reference to the next node.

Operations Algorithms

Operation

Description

Algorithm

1. Enqueue

This operation can be used to add the element to the rear of the queue.

  1. Check if the Queue is full.
  2. If Queue is full then display an overflow message as output.
  3. If Queue is not full then increment the rear pointer to the next position.
  4. Insert the new element at the rear position.
  5. Update the size of the queue.

2. Dequeue

This operation can be used to remove the element from the front of the queue.

  1. Check whether if the queue is empty or not.
  2. If queue is empty then display an underflow message as output.
  3. If queue is not empty then retrieve the element at the front position.
  4. Increment the front pointer to the next position.
  5. Update the size of the queue.
  6. Return the retrieved element as output.

3. Peek

It can be used to get the element at the front of the queue without removing it.

  1. Check whether if the queue is empty.
  2. If queue is empty then display an overflow message as output.
  3. If queue is not empty then return the element at front position without removing it.

4. isEmpty

It can be used to check if the queue is empty or not.

  1. Check whether if the size of the queue is equal to zero.
  2. if the size is 0 then it return true(that means queue is empty).
  3. Otherwise, returns false.

5. isFull

Check if the queue is full. (Is using Fixed Size Array)

  1. Check if size of the queue is equal to capacity of the array.
  2. If size is equal to capacity then return true.
  3. Otherwise, returns false.

Example program

Java
// Java Program to Implement
// Queue Data Structure

class Queue {
    private int[] arr;
    private int front;
    private int rear;
    private int capacity;
    private int size;
    
    // Constructor to initialize the queue
    public Queue(int capacity) {
        this.capacity = capacity;
        arr = new int[capacity];
        front = 0;
        rear = -1;
        size = 0;
    }
    
    // Insert an element at the rear of the queue
    public void enqueue(int item) {
        if (isFull()) {
            System.out.println("Queue is full");
            return;
        }
        rear = (rear + 1) % capacity;
        arr[rear] = item;
        size++;
    }
    
    // Remove and return the element from the front of the queue
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return -1;
        }
        int removedItem = arr[front];
        front = (front + 1) % capacity;
        size--;
        return removedItem;
    }
    
    // Return the element at the front of the queue without removing it
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return -1;
        }
        return arr[front];
    }
    
    // Check if the queue is empty
    public boolean isEmpty() {
        return size == 0;
    }
    
    // Check if the queue is full
    public boolean isFull() {
        return size == capacity;
    }
}

public class Main {
    public static void main(String[] args) {
        Queue queue = new Queue(5);
        queue.enqueue(10);
        queue.enqueue(20);
        queue.enqueue(30);
        System.out.println("Dequeued item: " + queue.dequeue());
        System.out.println("Front item: " + queue.peek());
        System.out.println("Is queue empty? " + queue.isEmpty());
    }
}

Output
Dequeued item: 10
Front item: 20
Is queue empty? false

Complexity of the above Program:

Time complexity: O(1)
Space complexity: O(n) where n is the number of the elements in the queue.

Application of the Queue

  • It can applies on the operating systems for the handling process scheduling.
  • It can be used in the print queue management in computer systems.
  • It can applies on the BFS(Breadth-First Search) algorithm in the graph traversal.
  • It can applies on the handling the requests in web servers.

Next Article
Article Tags :
Practice Tags :

Similar Reads