Java Program for Queue Simulation
Program
import java.io.*;
import java.util.*;
class queue {
private final int size = 5;
private int front, rear;
private int que[];
queue() {
front = 0;
rear = -1;
que = new int[size];
}
void insert(int item) {
if (rear == size - 1) {
System.out.println("Queue full");
return;
} else {
que[++rear] = item;
System.out.println("Element inserted is:\t" + que[rear]);
return;
}
}
void del() {
if (front > rear) {
System.out.println("Queue Empty");
return;
} else {
System.out.println("Element deleted is:\t" + que[front]);
front++;
}
if (front > rear) {
front = 0;
rear = -1;
}
}
void display() {
if (front > rear) {
System.out.println("Queue Empty");
return;
} else {
System.out.println("Contents of Queue:");
for (int i = front; i <= rear; i++)
System.out.println(que[i]);
return;
}
}
}
public class QueueSimulation {
public static void main(String[] args) {
queue q = new queue();
int choice;
Scanner sc = new Scanner(System.in);
System.out.println("QUEUES");
while (true) {
System.out.println("1. Insert\n2. Delete\n3. Display\n4. Exit");
System.out.println("Enter choice");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the item to insert");
int item = sc.nextInt();
q.insert(item);
break;
case 2:
q.del();
break;
case 3:
q.display();
break;
case 4:
sc.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
This program demonstrates queue simulation in Java using an array-based implementation. A queue operates on the FIFO (First In, First Out) principle, meaning elements are added to the rear and removed from the front. The program showcases the fundamental operations of a queue (insert
, delete
, display
). The queue is implemented using a fixed-size array with front
and rear
to track elements. The boundary conditions are handled in such a way that it prevents insertion when the queue is full and prevents deletion when the queue is empty.
Key Concepts of the Queue:
- Insert: Adds an element to the rear of the queue.
- Delete: Removes an element from the front of the queue.
- Display: Shows all the elements currently in the queue.
- Queue Full and Empty Conditions:
- The queue is full when the
rear
index reaches the maximum size. - The queue is empty when
front
exceedsrear
.
- The queue is full when the
1. Class queue
:
- Implements the queue operations using an array.
- Data Members:
size
: Maximum capacity of the queue (set to5
).front
: Tracks the front of the queue (initialized to0
).rear
: Tracks the rear of the queue (initialized to-1
).que[]
: Array to hold queue elements.
- Methods:
- Constructor (
queue()
):- Initializes
front
andrear
to their starting values. - Creates a queue array of fixed size (
size
).
- Initializes
insert(int item)
:- Adds an element to the rear of the queue if it is not full.
- If the queue is full (
rear == size - 1
), it prints "Queue full".
del()
:- Removes and returns the front element if the queue is not empty.
- If the queue is empty (
front > rear
), it prints "Queue Empty". - Resets
front
andrear
to initial values if all elements are deleted.
display()
:- Prints all the elements in the queue from
front
torear
. - If the queue is empty, it prints "Queue Empty".
- Prints all the elements in the queue from
- Constructor (
2. Class QueueSimulation
:
- Contains the
main()
method to simulate queue operations. - Steps:
- Creates an object of the
queue
class. - Presents a menu of operations:
- Insert: Add an element to the queue.
- Delete: Remove an element from the queue.
- Display: Show the current contents of the queue.
- Exit: Exit the program.
- The user selects an option, and the program calls the respective method of the
queue
class.
- Creates an object of the
Output
QUEUES
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
1
Enter the item to insert
10
Element inserted is: 10
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
1
Enter the item to insert
20
Element inserted is: 20
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
1
Enter the item to insert
30
Element inserted is: 30
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
1
Enter the item to insert
40
Element inserted is: 40
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
1
Enter the item to insert
50
Element inserted is: 50
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
1
Enter the item to insert
60
Queue full
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
2
Element deleted is: 10
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
2
Element deleted is: 20
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
2
Element deleted is: 30
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
3
Contents of Queue:
40
50
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
2
Element deleted is: 40
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
2
Element deleted is: 50
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
2
Queue Empty
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
3
Queue Empty
1. Insert
2. Delete
3. Display
4. Exit
Enter choice
4