Implementing Circular Queue in java

               A queue which has connected front end and rear end to form a circle. Let us implement this in java. Front end is the one where we remove the element. Rear end is the one where the data is inserted.

It has 5 operations list below.

  1. 1.       Initialize
  2. 2.       Enqueue
  3. 3.       Dequeue
  4. 4.       isEmpty()
  5. 5.       isFull()

1.initialize: The data structure ‘CircularQueue’ size is assigned with value ‘k’.

2.Enqueue: It inserts the elements to the rear end. If the queue is full,it gives you an exception.

3.Dequeue: It deletes the element in the front end. It gives you an exception,when your queue is empty.

4.isEmpty(): it checks for queue has no elements.

5.isFull(): It checks the queue is full or not.

Program:

public class CircularQueueEg {

    private int[] queue1;

    private int frontend;

    private int rearend;

    private int size;

    private int capacity;

 

    public CircularQueueEg(int k) {

        capacity = k;

        queue1 = new int[capacity];

        frontend = -1;

        rearend = -1;

        size = 0;

    }

 

    public boolean enqueueit(int value) {

        if (isFull()) {

            throw new IllegalStateException("The circular queue is full.");

        }

        if (isEmpty()) {

            frontend = 0;

        }

        rearend = (rearend + 1) % capacity;

        queue1[rearend] = value;

        size++;

        return true;

    }

 

    public int dequeueit() {

        if (isEmpty()) {

            throw new IllegalStateException("The circular queue is empty.");

        }

        int value = queue1[frontend];

        if (frontend == rearend) {

            frontend = -1;

            rearend = -1;

        } else {

            frontend = (frontend + 1) % capacity;

        }

        size--;

        return value;

    }

 

    public int frontend() {

        if (isEmpty()) {

            throw new IllegalStateException("The circular queue is empty.");

        }

        return queue1[frontend];

    }

 

    public int rearend() {

        if (isEmpty()) {

            throw new IllegalStateException("The circular queue is empty.");

        }

        return queue1[rearend];

    }

 

    public boolean isEmpty() {

        return size == 0;

    }

 

    public boolean isFull() {

        return size == capacity;

    }

 

    public int size() {

        return size;

    }

    public static void main(String[] args) {

        CircularQueueEg cq = new CircularQueueEg(6);

        cq.enqueueit(100);

        cq.enqueueit(210);

        cq.enqueueit(320);

        cq.enqueueit(450);

        cq.enqueueit(740);

       System.out.println(cq.dequeueit()); 

        System.out.println(cq.frontend());   

        cq.enqueueit(160);

        System.out.println(cq.rearend()); 

    }

}

Output:

C:\raji\blog>javac CircularQueueEg.java

C:\raji\blog>java CircularQueueEg

100

210

160

This is the way of implementing circular queue in java. Keep doing coding!!!

No comments:

Post a Comment