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:

  1. Insert: Adds an element to the rear of the queue.
  2. Delete: Removes an element from the front of the queue.
  3. Display: Shows all the elements currently in the queue.
  4. Queue Full and Empty Conditions:
    • The queue is full when the rear index reaches the maximum size.
    • The queue is empty when front exceeds rear.

1. Class queue:

  • Implements the queue operations using an array.
  • Data Members:
    • size: Maximum capacity of the queue (set to 5).
    • front: Tracks the front of the queue (initialized to 0).
    • rear: Tracks the rear of the queue (initialized to -1).
    • que[]: Array to hold queue elements.
  • Methods:
    1. Constructor (queue()):
      • Initializes front and rear to their starting values.
      • Creates a queue array of fixed size (size).
    2. 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".
    3. 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 and rear to initial values if all elements are deleted.
    4. display():
      • Prints all the elements in the queue from front to rear.
      • If the queue is empty, it prints "Queue Empty".

2. Class QueueSimulation:

  • Contains the main() method to simulate queue operations.
  • Steps:
    1. Creates an object of the queue class.
    2. 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.
    3. The user selects an option, and the program calls the respective method of the queue class.

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