Queue Operations Program in Java and Python
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.


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:
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:
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:
/**
* 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:
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:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.