ISC Practicals and Theory - Circular Queue
Updated 21 Oct 2026
A circular queue is a queue stored in an array in which the position after the last index is the first index, so the space freed by removed elements is used again. This is the classic ISC form: a default constructor, an init method that sets the capacity, void methods that print their own messages, and a menu in main to add, remove and display elements.
Definition
A queue works on the first-in first-out (FIFO) rule: elements are added at the rear and removed from the front. In a simple array queue, the slots at the start of the array are wasted once elements are removed. In a circular queue the array is treated as a ring: after index cap - 1 the next index is 0. This is done with the remainder operator:
$next = (index + 1) \% cap$
For a capacity of 5, if rear is 4 then (4 + 1) % 5 = 0, so the next element goes to index 0.
Convention used in this program:
front = -1andrear = -1mean the queue is empty.- The first element is stored at index 0, so
front = rear = 0. - The queue is full when the position after
rearisfront, that is(rear + 1) % cap == front. Allcapslots can be used. - When the last element is removed (
front == rear), both are set back to -1.
Capacity: it must be from 1 to 20. Any other value is rejected with a message.
Algorithm
- In
main, create the objectobj(the default constructor setsfront = rear = -1). Read the capacityc. Ifc < 1orc > 20, print an error message and stop. Otherwise callobj.init(c), which creates the arrayqof sizec. - Repeat the menu until the choice is 4: print the menu, read
choiceand call the matching method. For any other number printInvalid choice. Enter 1 to 4. add(): readv.- If
isFull()is true, print an overflow message. - Otherwise, if the queue is empty, set
front = 0andrear = 0; else setrear = (rear + 1) % cap. - Store
vinq[rear]and print a message.
- If
remove():- If
isEmpty()is true, print an underflow message. - Otherwise take
v = q[front]. Iffront == rearit was the last element, so setfront = -1andrear = -1; else setfront = (front + 1) % cap. Printv.
- If
display():- If the queue is empty, print a message.
- Otherwise start with
i = front, printq[i], movei = (i + 1) % cap, and repeat untilibecomes(rear + 1) % cap. This do-while loop also works when the queue is full, and whenrearis beforefrontafter wrap-around.
- When choice 4 is entered, print
Program ended.and the loop stops.
Program
init creates the array, isEmpty and isFull test the two limits, add and remove change rear and front, display prints from front to rear going round, and main runs the menu. One Scanner is created as a data member and shared by main and add, because two Scanners on the same input can lose typed data.
import java.util.Scanner;
public class CircularQueue
{
int q[]; // array that holds the elements of the queue
int cap; // capacity (maximum number of elements)
int front; // index of the front element; -1 when the queue is empty
int rear; // index of the rear element; -1 when the queue is empty
Scanner sc = new Scanner(System.in); // the only Scanner of the program, shared by all methods
// Default constructor: sets the data members to empty values
CircularQueue()
{
q = null;
cap = 0;
front = -1;
rear = -1;
}
// Creates the array of the given capacity and marks the queue empty
// Parameter c: the capacity
void init(int c)
{
cap = c;
q = new int[cap];
front = -1;
rear = -1;
}
// Returns true if the queue has no element
boolean isEmpty()
{
return front == -1;
}
// Returns true if the next position after rear (going round) is front
boolean isFull()
{
return (rear + 1) % cap == front;
}
// Reads an element and adds it at the rear; prints a message
void add()
{
System.out.print("Enter the element to add: ");
int v = sc.nextInt();
if (isFull())
{
System.out.println("Overflow: the queue is full.");
}
else
{
if (isEmpty())
{
front = 0; // first element goes to index 0
rear = 0;
}
else
{
rear = (rear + 1) % cap; // move rear one step, going back to 0 after cap - 1
}
q[rear] = v;
System.out.println("Element added: " + v);
}
}
// Removes the element at the front and prints it; prints a message if empty
void remove()
{
if (isEmpty())
{
System.out.println("Underflow: the queue is empty.");
}
else
{
int v = q[front];
if (front == rear)
{
front = -1; // the last element is removed: queue becomes empty
rear = -1;
}
else
{
front = (front + 1) % cap; // move front one step, going back to 0 after cap - 1
}
System.out.println("Removed element: " + v);
}
}
// Displays the elements from front to rear, going round the array
void display()
{
if (isEmpty())
{
System.out.println("The queue is empty.");
}
else
{
System.out.print("Queue (front to rear): ");
int i = front;
do
{
System.out.print(q[i] + " ");
i = (i + 1) % cap;
} while (i != (rear + 1) % cap);
System.out.println();
}
}
// main: reads the capacity, then runs the menu until Exit is chosen
public static void main(String[] args)
{
CircularQueue obj = new CircularQueue();
System.out.print("Enter the capacity of the queue (1 to 20): ");
int c = obj.sc.nextInt();
if (c < 1 || c > 20)
{
System.out.println("Invalid capacity. It must be from 1 to 20.");
return;
}
obj.init(c);
int choice;
do
{
System.out.println();
System.out.println("1. Add");
System.out.println("2. Remove");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = obj.sc.nextInt();
switch (choice)
{
case 1: obj.add(); break;
case 2: obj.remove(); break;
case 3: obj.display(); break;
case 4: System.out.println("Program ended."); break;
default: System.out.println("Invalid choice. Enter 1 to 4.");
}
} while (choice != 4);
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
q (data member) | int[] | Array that stores the elements of the queue |
cap (data member) | int | Capacity: the maximum number of elements |
front (data member) | int | Index of the front element; -1 when the queue is empty |
rear (data member) | int | Index of the rear element; -1 when the queue is empty |
sc (data member) | Scanner | Scanner shared by all methods to read input |
c (in init) | int | Parameter: the capacity to set |
v (in add) | int | Element read from the user to be added |
v (in remove) | int | Element taken from the front, printed after removal |
i (in display) | int | Index that moves from front to rear going round the array |
args | String[] | Parameter: command line arguments of main |
obj | CircularQueue | Object on which the methods are called |
c (in main) | int | Capacity entered by the user |
choice | int | Menu choice entered by the user |
Sample input and output
Sample 1
Enter the capacity of the queue (1 to 20): 3
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 10
Element added: 10
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 20
Element added: 20
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 30
Element added: 30
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 40
Overflow: the queue is full.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 3
Queue (front to rear): 10 20 30
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 2
Removed element: 10
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 2
Removed element: 20
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 50
Element added: 50
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 60
Element added: 60
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 3
Queue (front to rear): 30 50 60
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 4
Program ended.Sample 2
Enter the capacity of the queue (1 to 20): 1
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 2
Underflow: the queue is empty.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 5
Element added: 5
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 6
Overflow: the queue is full.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 3
Queue (front to rear): 5
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 2
Removed element: 5
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 3
The queue is empty.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 9
Invalid choice. Enter 1 to 4.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 4
Program ended.Sample 3
Enter the capacity of the queue (1 to 20): 25
Invalid capacity. It must be from 1 to 20.Sample 4
Enter the capacity of the queue (1 to 20): 4
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 2
Underflow: the queue is empty.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 3
The queue is empty.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 1
Enter the element to add: 8
Element added: 8
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 3
Queue (front to rear): 8
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 2
Removed element: 8
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 2
Underflow: the queue is empty.
1. Add
2. Remove
3. Display
4. Exit
Enter your choice: 4
Program ended.Dry run
Take the first sample with capacity 3. The array is shown with _ for an empty slot.
| Operation | front | rear | q[0], q[1], q[2] | Note |
|---|---|---|---|---|
| Start | -1 | -1 | _, _, _ | Empty |
| Add 10 | 0 | 0 | 10, _, _ | Queue was empty |
| Add 20 | 0 | 1 | 10, 20, _ | rear = (0 + 1) % 3 = 1 |
| Add 30 | 0 | 2 | 10, 20, 30 | (2 + 1) % 3 = 0 = front: full |
| Add 40 | 0 | 2 | 10, 20, 30 | Overflow, nothing changes |
| Remove | 1 | 2 | _, 20, 30 | Removes 10, front = (0 + 1) % 3 = 1 |
| Remove | 2 | 2 | _, _, 30 | Removes 20 |
| Add 50 | 2 | 0 | 50, _, 30 | rear = (2 + 1) % 3 = 0: freed slot reused |
| Add 60 | 2 | 1 | 50, 60, 30 | rear = (0 + 1) % 3 = 1; now full as (1 + 1) % 3 = 2 = front |
Display: i = 2 prints 30; i = (2 + 1) % 3 = 0 prints 50; i = 1 prints 60; then i = 2, which equals (rear + 1) % cap = 2, so the loop stops. The output is 30 50 60, which matches the first sample run.
Variations
- Count variable "Write a circular queue program that keeps a count of the elements instead of testing front and rear for fullness."
- Free slots "Modify the circular queue program to also display the number of free slots after every operation."
- Dequeue "Write a menu driven program for a dequeue that allows insertion and deletion at both ends."
- Queue of names "Implement a circular queue of names using a String array."