import java.util.
*;
class Node
int data; // Value of the node
Node next; // Reference to the next node
public Node(int data)
this.data = data;
this.next = null;
// Queue class implemented using a linked list
class QueueLinkedList
Node front, rear;
public QueueLinkedList()
this.front = this.rear = null;
// Enqueue method to add an element to the rear of the queue
public void enqueue(int value)
Node newNode = new Node(value); // Create a new node
if (rear == null)
front = rear = newNode;
System.out.println(value + " enqueued to queue.");
return;
rear.next = newNode;
rear = newNode;
System.out.println(value + " enqueued to queue.");
// Dequeue method to remove an element from the front of the queue
public int dequeue()
if (front == null)
System.out.println("Queue is empty. Cannot dequeue.");
return -1;
int dequeuedValue = front.data;
front = front.next;
if (front == null)
rear = null;
System.out.println(dequeuedValue + " dequeued from queue.");
return dequeuedValue;
// Display method to show all elements in the queue
public void display()
if (front == null)
System.out.println("Queue is empty.");
return;
Node current = front;
System.out.println("Queue elements:");
while (current != null) {
System.out.println(current.data); // Print each element
current = current.next;
// Main class to demonstrate queue operations
public class QueueMain
public static void main()
Scanner in = new Scanner(System.in);
QueueLinkedList queue = new QueueLinkedList(); // Create a QueueLinkedList object
while (true)
System.out.println("Choose an operation:");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Display");
System.out.println("4. Exit");
int choice = in.nextInt(); // Read user choice
switch (choice)
case 1:
System.out.println("Enter a number to enqueue:");
int value = in.nextInt();
queue.enqueue(value); // Enqueue the user input
break;
case 2:
queue.dequeue(); // Dequeue the front element
break;
case 3:
queue.display(); // Display the queue
break;
case 4:
System.out.println("Exiting program.");
in.close();
return;
default:
System.out.println("Invalid choice. Please try again.");