Program opened below. You can scroll through the algorithm, Java code and output.

Circular Queue

Download .java

Question

Implement a circular queue using an array with insertion, deletion and display operations.

Step 1: Start the program and declare the array for the circular queue. Step 2: Initialise front and rear to -1 to indicate an empty queue. Step 3: Display the menu for insertion, deletion, display and exit. Step 4: Accept the user's menu choice. Step 5: For insertion, test whether the next rear position would equal front. Step 6: If the queue is full, display an overflow message. Step 7: Otherwise set front to zero if the queue is empty, advance rear circularly and store the item. Step 8: For deletion, check whether front is -1. Step 9: If the queue is empty, display an underflow message. Step 10: Otherwise display the front item and move front to the next circular position. Step 11: If the deleted item was the only item, reset both pointers to -1. Step 12: For display, traverse from front to rear using circular indexing and print each item on a separate line. Step 13: Repeat the menu until EXIT is selected, then stop.
Java
import java.util.*;
class CircularQueue
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int[] q = new int[5];
        int front = -1, rear = -1, choice, item;
        do
        {
            System.out.println("1.Insert  2.Delete  3.Display  4.Exit");
            choice = sc.nextInt();
            if (choice == 1)
            {
                if ((rear + 1) % 5 == front)
                {
                    System.out.println("Queue Overflow");
                }
                else
                {
                    System.out.print("Item: ");
                    item = sc.nextInt();
                    if (front == -1)
                    {
                        front = 0;
                    }
                    rear = (rear + 1) % 5;
                    q[rear] = item;
                }
            } else if (choice == 2)
            {
                if (front == -1)
                {
                    System.out.println("Queue Underflow");
                }
                else
                {
                    System.out.println("Deleted: " + q[front]);
                    if (front == rear)
                    {
                        front = rear = -1;
                    }
                    else
                    {
                        front = (front + 1) % 5;
                    }
                }
            } else if (choice == 3)
            {
                if (front == -1)
                {
                    System.out.println("Queue is empty");
                }
                else
                {
                    int i = front;
                    while (true)
                    {
                        System.out.println(q[i]);
                        if (i == rear)
                        {
                            break;
                        }
                        i = (i + 1) % 5;
                    }
                }
            }
        } while (choice != 4);
    }
}
Input: Operations: Insert 11, Insert 22, Display, Exit Output: Queue display: 11 22
← Previous Program 41 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.