When to make use of Queue over ArrayList in Java?

[ad_1]

import java.util.*;

  

class GFG {

    int arr[], entrance, rear, cap, n1;

  

    

    GFG(int n)

    {

        arr = new int[n];

        cap = n;

        entrance = 0;

        rear = -1;

        n = 0;

    }

  

    

    

    public void dequeue()

    {

  

        

        if (isEmpty()) {

            System.out.println(

                "No gadgets within the queue, can not delete");

            System.exit(1);

        }

  

        System.out.println("Deleting " + arr[front]);

  

        entrance = (entrance + 1) % cap;

        n1--;

    }

  

    

    

    public void enqueue(int val)

    {

  

        

        if (isFull()) {

            System.out.println(

                "OverFlow!!Can not add extra values");

            System.exit(1);

        }

  

        System.out.println("Including " + val);

  

        rear = (rear + 1) % cap;

        arr[rear] = val;

        n1++;

    }

  

    

    

    public int peek()

    {

        if (isEmpty()) {

            System.out.println(

                "Queue empty!!Can not delete");

            System.exit(1);

        }

        return arr[front];

    }

  

    

    public int dimension() { return n1; }

  

    

    

    public Boolean isEmpty() { return (dimension() == 0); }

  

    

    

    public Boolean isFull() { return (dimension() == cap); }

  

    

    public static void essential(String[] args)

    {

  

        

        GFG q = new GFG(5);

  

        q.enqueue(10);

        q.enqueue(20);

        q.enqueue(30);

  

        System.out.println("Entrance component is: " + q.peek());

        q.dequeue();

        System.out.println("Entrance component is: " + q.peek());

  

        System.out.println("Queue dimension is " + q.dimension());

  

        q.dequeue();

        q.dequeue();

  

        if (q.isEmpty())

            System.out.println("Queue Is Empty");

        else

            System.out.println("Queue Is Not Empty");

    }

}

[ad_2]

Leave a Reply