SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Queue Operations Program in Java and Python

22 February 2015

Queue insertion and deletion program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to implement a linear queue using an array. The program should allow insertion from the rear end, deletion from the front end and display of the queue elements.

A queue follows the FIFO rule: First In, First Out. Overflow occurs when insertion is attempted in a full queue, and underflow occurs when deletion is attempted from an empty queue.

Queue insertion from rear
Insertion is performed from the rear end of the queue.
Queue deletion from front
Deletion is performed from the front end of the queue.
SAMPLE OPERATION TRACE: Create queue of size 5 Insert 10 Insert 20 Insert 30 Delete Display OUTPUT: Deleted element: 10 The elements in the queue are: 20 30

Algorithm:

Step 1: Start.

Step 2: Accept the maximum queue size and create array Q.

Step 3: Initialize front = 0 and rear = 0 to represent an empty queue.

Step 4: For insertion, first check whether rear == size.

Step 5: If the condition is true, display OVERFLOW; otherwise store the value at Q[rear] and increase rear.

Step 6: For deletion, first check whether front == rear.

Step 7: If the queue is empty, display UNDERFLOW and return a sentinel value.

Step 8: Otherwise store Q[front] in a temporary variable and increase front.

Step 9: If front becomes equal to rear, reset both indexes to 0 so the queue becomes reusable from the beginning.

Step 10: For display, print all elements from index front to rear - 1.

Step 11: Repeat menu operations until the user chooses to exit.

Step 12: Stop.

Explanation:

The array Q stores queue elements in the order in which they are inserted. The variable rear always points to the next free position for insertion, while front points to the element that will be deleted next.

During insertion, the program checks rear == size. If this is true, the array has no free position at the rear end, so the queue cannot accept another value. Otherwise the value is stored and rear is increased by one.

During deletion, the condition front == rear means that no valid element exists between the two indexes. If an element is available, the program saves Q[front], increases front, and returns the removed value.

After the last element is deleted, both indexes are reset to 0. This keeps the simple linear-queue representation clean for the next set of insertions.

A queue follows the FIFO principle, meaning the first value inserted is the first one removed. The program usually maintains front and rear indexes. Insertion takes place at the rear, while deletion takes place from the front. Overflow must be checked before inserting into a full array, and underflow must be checked before deleting from an empty queue. After deletion, either the front index moves forward or elements are shifted depending on the implementation. These index changes are the key to preserving queue order.

Java Program:

Insertion Operation:

Java
void insert(int v) // Function to insert element in Queue
{
    if(rear == size) // Condition for Overflow
    {
        System.out.println("OVERFLOW");
    }
    else
    {
        Q[rear] = v; // Storing value in Queue
        rear = rear + 1;
    }
}

Deletion Operation:

Java
int delete() // Function to delete element from Queue
{
    if(front == 0 && rear == 0) // Condition for Underflow
    {
        System.out.println("UNDERFLOW");
        return -999;
    }
    else
    {
        int val = Q[front]; // Storing the element which will be removed
        front = front + 1;
        if(front == rear) // Condition for emptying the Queue
        {
            front = 0;
            rear = 0;
        }
        return val;
    }
}

Complete Java Program Implementing Operations on Queue:

Java
/**
* The class Queue implements operations of queue using Java
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

class Queue
{
    int Q[]; // Array to implement Queue
    int size; // Maximum size of the Queue
    int front; // Index of front element
    int rear; // Index of rear element

    Queue(int cap) // Parameterised Constructor
    {
        size = cap;
        Q = new int[size];
        front = 0;
        rear = 0;
    }

    void insert(int v) // Function to insert element in Queue
    {
        if(rear == size) // Condition for Overflow
        {
            System.out.println("OVERFLOW");
        }
        else
        {
            Q[rear] = v; // Storing value in Queue
            rear = rear + 1;
        }
    }

    int delete() // Function to delete element from Queue
    {
        if(front == 0 && rear == 0) // Condition for Underflow
        {
            System.out.println("UNDERFLOW");
            return -999;
        }
        else
        {
            int val = Q[front]; // Storing the element which will be removed
            front = front + 1;
            if(front == rear) // Condition for emptying the Queue
            {
                front = 0;
                rear = 0;
            }
            return val;
        }
    }

    void display() // Function for printing elements in the queue
    {
        if(front == 0 && rear == 0)
        {
            System.out.println("The Queue is empty");
        }
        else
        {
            System.out.println("The elements in the queue are : ");
            for(int i=front; i<rear; i++)
            {
                System.out.println(Q[i]);
            }
        }
    }
}

Equivalent Python Program:

Python
class QueueOperations:
    def __init__(self, cap):
        self.size = cap
        self.Q = [0] * self.size
        self.front = 0
        self.rear = 0

    def insert(self, v):
        # rear points to the next free position for insertion.
        if self.rear == self.size:
            print("OVERFLOW")
        else:
            self.Q[self.rear] = v
            self.rear = self.rear + 1

    def delete(self):
        # front == rear means there is no element to remove.
        if self.front == self.rear:
            print("UNDERFLOW")
            return -999

        val = self.Q[self.front]
        self.front = self.front + 1

        # Reset indexes when the queue becomes empty.
        if self.front == self.rear:
            self.front = 0
            self.rear = 0
        return val

    def display(self):
        if self.front == self.rear:
            print("The Queue is empty")
        else:
            print("The elements in the queue are:")
            for i in range(self.front, self.rear):
                print(self.Q[i], end=" ")
            print()


n = int(input("Enter queue size: "))
ob = QueueOperations(n)

while True:
    print("1. Insert  2. Delete  3. Display  4. Exit")
    ch = int(input("Enter your choice: "))

    if ch == 1:
        v = int(input("Enter value to insert: "))
        ob.insert(v)
    elif ch == 2:
        d = ob.delete()
        if d != -999:
            print("Deleted element:", d)
    elif ch == 3:
        ob.display()
    elif ch == 4:
        break
    else:
        print("Invalid choice")

Output:

Enter queue size: 5 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter value to insert: 10 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter value to insert: 20 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 2 Deleted element: 10 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 The elements in the queue are: 20

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →