Insertion in a Queue in Java

Java Program for insertion in a Queue

How to insert an element in a Queue using JAVA programming?

Insertion in a queue in java is a simple operation that we can perform on Queue Data Structure. Insertion in a Queue is also known as Enqueue operation when we are talking in respect with Queues. There are several different types of Queues, which we learn afterwards. In this article lets learn how to code a C++ Program for Inserting an Element in a Queue

Steps to
insert an
element in a
Queue in Java

Here we will be discussing the steps to fill an empty queue whose size and data will be user input

  1. In the main function we will take size of queue as user input.
  2. And then we will initialize a for loop that will iterate till the size of queue.
  3. On every iteration we will ask the data from the user and will insert that data in the queue using enqueue function.
  4. In the enqueue function first we will increase the value of rear by one on every successful iteration.
  5. And will insert data at rear of the queue.
  6. Later we will use the display function to display the data that is present in the queue.
Java Program for Enqueue Operation in a Queue

to insert an
element in a
Queue in Java

Following algorithm is followed when an element is to be entered in a Queue

  • if(rear == MAX-1)
    • Print “Queue is full”
  • else
    • rear = rear + 1
    • queue[rear]=data
    • if(front== -1)
      • front=0

Java programming code for insertion in a Queue(Enqueue)

import java.util.Scanner;

public class Prepinsta {
	public final static int MAX = 50;/*Defining the max size of the 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 elements 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] + "  ");

	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();
		for(int i = 0; i < size; i++) 
			System.out.println("\nEnter Data to Enqueue:");
			data = STDIN_SCANNER.nextInt();
			System.out.println("Enqueuing " + data);

	public final static Scanner STDIN_SCANNER = new Scanner(;
Enter the size of queue: 5

Enter Data to Enqueue:
Enqueuing 5

The elements in the queue are:5  

Enter Data to Enqueue:
Enqueuing 10

The elements in the queue are:5  10  

Enter Data to Enqueue:
Enqueuing 15

The elements in the queue are:5  10  15  

Enter Data to Enqueue:
Enqueuing 20

The elements in the queue are:5  10  15  20  

Enter Data to Enqueue:
Enqueuing 25

The elements in the queue are:5  10  15  20  25

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