Queue : An easy Data Structure in java
Queue: solved Java program for ISC Class 11 Computer Science with complete BlueJ code, explanation and sample output.
Watch java videos
Queue : Data Structure is a way to collect and organize data in such a way that we can perform operations on these data in an effective way.
It is a way to store and organize data so that it can be used efficiently. In computer science, it is a data organization, management, and storage format that enables efficient access and modification.
In this post we will discuss the Queue data structure. It follows the FIFO(First In First Out) principal. In this data structure the elements are inserted and deleted from the same end. It is a data structure where the element that is inserted the first is removed the first. The end from where the elements are inserted is called rear. The end from where the elements are removed is called front.
Enqueue : When an element is inserted in the queue, it is called enqueue.
Dequeue : When an element is removed from the queue, this process is called dequeue.
Applications of queue: It is used to CPU scheduling, Disk scheduling,

import java.io.*;
public class Queue
{
int Q[];
int size;
int front;
int rear;
Queue(int cap)
{
size = cap;
Q = new int[size];
front = 0;
rear = 0;
}
void insert(int v)
{
if(rear == size)
{
System.out.println("OVERFLOW");
}
else
{
Q[rear] = v;
rear = rear + 1;
}
}
int delete()
{
if(front == 0 && rear == 0)
{
System.out.println("UNDERFLOW");
return -999;
}
else
{
int val = Q[front];
front = front + 1;
if(front == rear)
{
front = 0;
rear = 0;
}
return val;
}
}
void display()
{
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]);
}
}
}
public static void main()throws IOException
{
System.out.println("************************************ Queue ***********************************");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of Queue:"); int n=Integer.parseInt(br.readLine()); Queue obj=new Queue(n); int ch, gr, t;
do
{
System.out.println("Enter 1 to push element");
System.out.println("Enter 2 to remove element"); System.out.println("Enter 3 to print list"); System.out.println("Enter 0 to Exit"); System.out.println("Enter your choice"); ch=Integer.parseInt(br.readLine()); switch(ch)
{
case 1:
System.out.println("Enter the value");
gr=Integer.parseInt(br.readLine());
obj.insert(gr);
break;
case 2:
gr=obj.delete();
System.out.println("Obtained Value:"+gr);
System.out.println("Enter 1 to continue");
t=Integer.parseInt(br.readLine());
break;
case 3:
obj.display();
System.out.println("Press 1 to continue");
t=Integer.parseInt(br.readLine());
break;
case 0:
System.out.println("Thanks For Operating"); break;
default:
System.out.println("Re-enter your choice");
}
}
while(ch!=0);
}
}Output:
********************************Queue *********************************
Enter size of Queue:
4
Enter 1 to push element
Enter 2 to remove element
Enter 3 to print list
Enter 0 to Exit
Enter your choice
1
Enter the value
45
Enter 1 to push element
Enter 2 to remove element
Enter 3 to print list
Enter 0 to Exit
Enter your choice
1
Enter the value
67
Enter 1 to push element
Enter 2 to remove element
Enter 3 to print list
Enter 0 to Exit
Enter your choice
3
The elements in the queue are :
45
67
Press 1 to continue
Array programs
Related Posts
Continue learning with these related articles

ISC Practical 2027: College Enrolment Code Validator and Stream Report – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 question: validate college enrolment codes made of a stream, year and serial number, give the exact reason for each rejection, auto-correct lowercase streams and prepare a stream-wise report.

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

Boolean Algebra for ISC Class 12: Laws, Canonical SOP and POS, and Simplification
ISC Class 12 Computer Science notes on Boolean algebra: all the laws and theorems, duality, De Morgan's laws, minterms and maxterms, converting to canonical SOP and POS, and step-by-step algebraic simplification with solved examples.