Dremendo Tag Line

Double Ended Queue in Java Programming

Data Structure in Java

In this lesson, we will understand what is Double Ended Queue in Java Programming and how to create them along with some examples.

What is Double Ended Queue in Java

A Double Ended Queue in Java, also known as Deque, is a queue data structure in which insertion and deletion can be done from both left and right ends.

c double ended queue example

From the above image of the deque, we can see that when we add an element from the rear end, the R moves towards the right and, when we delete an element from the rear end, the R moves towards the left.

Similarly, when we delete an element from the front end, the F moves towards the right and when we add an element from the front end, the F moves towards the left.

video-poster

Operation on Double Ended Queue

There are four operations possible on the double ended queue.

  • Add Rear When we add an element from the rear end.
  • Delete Rear When we delete an element from the rear end.
  • Add Front When we add an element from the front end.
  • Delete Front When we delete an element from the front end.

Note: We will use the circular array to implement the Double Ended Queue.

Add Rear Operation in Deque

For adding an element from the rear end first, we check if the value of te is equal to the size of the array then, we will display a message Queue is full, else we will increase the value of R by R=(R+1)%Size and add the element in the array at the new location of R and then increased the value of te by 1.

Here, te is a variable that keeps the total number of elements present in the array. When an element is added, the value of te is increased by 1. When an element has deleted, the value of te is decreased by 1.

Example

if(te==size)
{
    System.out.println("Queue is full");
}
else
{
    R=(R+1)%size;
    arr[R]=new_item;
    te=te+1;
}

Delete Rear Operation in Deque

For deleting an element from the rear end first, we check if the value of te is equal to 0 then, we will display a message Queue is empty, else we will check if the value of R is -1, then we will initialize the value of R=size-1. After that, we will display the deleted element on the screen and decrease the value of R and te by 1.

Example

if(te==0)
{
    System.out.println("Queue is empty");
}
else
{
    if(R==-1)
    {
        R=size-1;
    }
    System.out.println("Number Deleted From Rear End = " + arr[R]);
    R=R-1;
    te=te-1;
}

Add Front Operation in Deque

For adding an element from the front end first, we check if the value of te is equal to the size of the array then, we will display a message Queue is full, else we will check if the value of F is 0 then we will initialize the value of F=size-1, else decrease the value of F by 1. After that, add the element in the array at the new location of F and then increased the value of te by 1.

Example

if(te==size)
{
	System.out.println("Queue is full");
}
else
{
    if(F==0)
    {
        F=size-1;
    }
    else
    {
        F=F-1;
    }
    arr[F]=new_item;
    te=te+1;
}

Delete Front Operation in Deque

For deleting an element from the front end first, we check if the value of te is equal to 0 then, we will display a message Queue is empty, else we will display the deleted element on the screen and then we will increase the value of F by F=(F+1)%Size and then decrease the value of te by 1.

Example

if(te==0)
{
    System.out.println("Queue is empty");
}
else
{
    System.out.println("Number Deleted From Front End = " + arr[F]);
    F=(F+1)%size;
    te=te-1;
}

Program of Deque using Circular Array

Below is the complete program of double ended queue in Java using a circular array having size 5.

Deque Program in Java using Circular Array

import java.util.Scanner;

public class Example
{
    public static void main(String args[])
    {
        int size=5;
        int arr[]=new int[size],R=-1,F=0,te=0,ch,n,i,x;
        Scanner sc=new Scanner(System.in);

        for(;;)		// An infinite loop
        {
            System.out.println("F=" + F + "  R=" + R);
            System.out.println("\n\n1. Add Rear");
            System.out.println("2. Delete Rear");
            System.out.println("3. Add Front");
            System.out.println("4. Delete Front");
            System.out.println("5. Display");
            System.out.println("6. Exit");
            System.out.print("Enter Choice: ");
            ch=sc.nextInt();

            switch(ch)
            {
                case 1:
                    if(te==size)
                    {
                        System.out.println("Queue is full");
                    }
                    else
                    {
                        System.out.print("Enter a number ");
                        n=sc.nextInt();
                        R=(R+1)%size;
                        arr[R]=n;
                        te=te+1;
                    }
                    break;

                case 2:
                    if(te==0)
                    {
                        System.out.println("Queue is empty");
                    }
                    else
                    {
                        if(R==-1)
                        {
                            R=size-1;
                        }
                        System.out.println("Number Deleted From Rear End = " + arr[R]);
                        R=R-1;
                        te=te-1;
                    }
                    break;

                case 3:
                    if(te==size)
                    {
                        System.out.println("Queue is full");
                    }
                    else
                    {
                        System.out.println("Enter a number ");
                        n=sc.nextInt();
                        if(F==0)
                        {
                            F=size-1;
                        }
                        else
                        {
                            F=F-1;
                        }
                        arr[F]=n;
                        te=te+1;
                    }
                    break;

                case 4:
                    if(te==0)
                    {
                        System.out.println("Queue is empty");
                    }
                    else
                    {
                        System.out.println("Number Deleted From Front End = " + arr[F]);
                        F=(F+1)%size;
                        te=te-1;
                    }
                    break;

                case 5:
                    if(te==0)
                    {
                        System.out.println("Queue is empty");
                    }
                    else
                    {
                        x=F;
                        for(i=1; i<=te; i++)
                        {
                            System.out.print(arr[x] + " ");
                            x=(x+1)%size;
                        }
                        System.out.println();
                    }
                    break;

                case 6:
                    System.exit(0);
                    break;

                default:
                    System.out.println("Wrong Choice");
            }
        }
    }
}