Implementing Circular Queue in Java with Arrays

A circular queue is an abstract data type that contains a collection of data which allows addition of data at the end of the queue and removal of data at the beginning of the queue. Circular queues have a fixed size.

Circular queue follows FIFO principle. Queue items are added at the rear end and the items are deleted at front end of the circular queue.

import java.io.*;
class Quec {
    public static int front, rear, n, que[];
    public Quec(int p) {
        n = p;
        que = new int[n];
        front = 0;
        rear = -1;
    }
    public static boolean isFull() {
        if (rear == n - 1)
            return true;
        else
            return false;
    }
    public static void enque() throws IOException {
        if (isFull())
            System.out.println("No Space");
        else {
            int temp;
            System.out.println("Enter an element");
            BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
            temp = Integer.parseInt(obj.readLine());
            rear++;
            que[rear] = temp;
        }
    }
    public static void display() {
        if (isEmpty())
            System.out.println("Queue is empty");
        else {
            for (int i = front; i <= rear; i++) {
                System.out.print(que[i] + " ");
            }
        }
    }
    public static boolean isEmpty() {
        if (rear < front)
            return true;
        else
            return false;
    }
    public static void deque() {
        if (isEmpty())
            System.out.println("Queue is empty");
        else {
            System.out.println("Removed Element" + que[front]);
            front++;
        }
    }
    public static void peek() {
        if (isEmpty())
            System.out.println("Queue is empty");
        else
            System.out.println("First Element is :" + que[front]);
    }
}
class Que {
    public static void main(String args[]) throws IOException {
        int n;
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter length of queue");
        n = Integer.parseInt(obj.readLine());
        Quec q = new Quec(n);
        int op = 0;
        char ch = ' ';
        do {
            System.out.println();
            System.out.println("Select operation to be performed");
            System.out.println("1.Insert an element in queue");
            System.out.println("2.Remove an element from queue");
            System.out.println("3.Display all Elements in queue");
            System.out.println("4.Peek Element");
            System.out.println("5.Exit");
            op = Integer.parseInt(obj.readLine());
            switch (op) {
                case 1:
                    Quec.enque();
                    break;
                case 2:
                    Quec.deque();
                    break;
                case 3:
                    Quec.display();
                    break;
                case 4:
                    Quec.peek();
                    break;
                case 5:
                    ch = 'Q';
                    break;
            }
        }
        while (ch != 'Q');
    }
}

/* Output
Enter length of queue

4


Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

1

Enter an element

10


Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

1

Enter an element

20


Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

1

Enter an element

30


Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

1

Enter an element

40


Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

1

No Space



Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

3

10 20 30 40 

Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

2

Removed Element10



Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

3

20 30 40 

Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

2

Removed Element20



Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

3

30 40 

Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

4

First Element is :30



Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

2

Removed Element30



Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

2

Removed Element40



Select operation to be performed

1.Insert an element in queue

2.Remove an element from queue

3.Display all Elements in queue

4.Peek Element

5.Exit

2

Queue is empty


Select operation to be performed
1.Insert an element in queue
2.Remove an element from queue
3.Display all Elements in queue
4.Peek Element
5.Exit
3

Queue is empty
*/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.