Deletion in a Queue in Java

How to delete an element from a queue using JAVA programming?

Deletion in a queue in java take place from the front of the queue as Queue follows the principle of FIFO that is the element that is inserted first will be the first to get deleted. The process of deleting an element from a queue is also termed as Dequeue.

In this article, we will be following the steps and algorithm to write a program for dequeue (removing a element from the queue). 

Steps to
delete an
element in a
Queue in Java

To delete the data from the queue that is in the process of dequeue following steps are followed

  1. Check the position of front, if front is at -1 or front is at rear+1 , then queue is empty
  2. Else increase the position of the front by one.
  3. Now use the display function to display the data.
  4. In that display function again check if the queue has some data or not.
  5. If queue has some data in it, then use a for loop to display the data left after dequeuing.
Java Program for Dequeue Operation in a Queue

Algorithm
to delete an
element in a
Queue in Java

When we want to remove an element from the queue, we need to follow the following steps

  • if(front==-1||front==rear+1)
    • Return “Queue is Empty”
  • else
    • queue[front]=0
    • front=front+1
  • disp()
*disp() is a function that is used to display the elements of the queue

Java programming code for deletion in a Queue(Dequeue)

import java.util.Scanner;

public class Main {
	
	public final static int MAX = 50;//defining max size of queue

	public static int[] queue = new int[MAX];

	public static int front = -1, rear = -1;

	public static void enqueue(int data) /* function to enqueue data */ 
        {
		if(rear == MAX - 1) {
			System.out.print("\nQueue is Full!");
		} else {
			rear = rear + 1;
			queue[rear] = data;
			if(front == -1) {
				front = 0;
			}
		}
	}

	public static void disp() /* function to display the relents of the queue */ 
        {
		System.out.print("\nThe elements in the queue are:");
		if(front == -1) {
			System.out.print("\nQueue is Empty");
		} else {
			for(int i = front; i <= rear; i++) {
				System.out.print(queue[i] + " ");
			}
		}
		System.out.println();
	}

	public static void dequeue() /* function to delete elements from the queue */
        {
		if(front == -1 || front == rear + 1) {
			System.out.print("\nQueue is Empty");
		} else {
			queue[front] = 0;
			front = front + 1;
		}
		disp();
	}

	public static void main(String[] args) /* main function for all input and output statements */ 
        {
		int data, size;
		System.out.print("Enter the size of queue:");
		size = STDIN_SCANNER.nextInt();
		System.out.println("\nEnter Data to Enqueue:");
		for(int i = 0; i < size; i++) {
			data = STDIN_SCANNER.nextInt();
			enqueue(data);
		}
		disp();
		System.out.println("\nDequeuing:");
	
		{
			dequeue();
		}
	}

	public final static Scanner STDIN_SCANNER = new Scanner(System.in);
}
Output:
Enter the size of queue: 5

Enter Data to Enqueue:
10
20
30
40
50

The elements in the queue are:10 20 30 40 50 

Dequeuing:

The elements in the queue are:20 30 40 50

Learn to code the similar program using C and C++

FAQ

Most frequent questions and answers
We can use following condition to check if there are some element present in the queue or not. front == -1 || front == rear + 1

If the given queue is empty then it will arise the underflow condition. The underflow condition says that the given queue is empty and we cannot delete more data from it.