/Circular Queue

Circular Queue

Circular Queue

public class CircularQueue {

    private int[] a;
    private int start = -1;
    private int end = -1;
    private int startX = -1;
    private int endX = -1;
    private int size = 0;

    public CircularQueue(int size) {
        this.size = size;
        a = new int[size];
    }

    public void enque(int data) {
        if (startX == -1) {
            a[++start] = data;
            a[++end] = data;
            startX += 1;
            endX += 1;
        } else {
            if (!isQueueFull()) {
                if (end == a.length - 1) {
                    end = -1;
                    a[++end] = data;
                    endX += 1;
                } else {
                    a[++end] = data;
                    endX += 1;
                }
            } else {
                System.out.println("Queue is Full");
            }
        }
    }

    private boolean isQueueFull() {
        return (endX - startX == a.length - 1) ? true : false;
    }

    private boolean isQueueEmpty() {
        return (endX == startX) ? true : false;
    }

    public void deque() {
        if (!isQueueEmpty()) {
            if (start == a.length - 1) {
                start = -1;
                start += 1;
                startX += 1;
            } else {
                start += 1;
                startX += 1;
            }
        } else {
            System.out.println("Queue is Empty");
        }
    }

    public void print() {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}