diff --git a/cir_linkedlist.java b/cir_linkedlist.java index f465e73..330d5dd 100644 --- a/cir_linkedlist.java +++ b/cir_linkedlist.java @@ -1,92 +1,92 @@ -import java.util.*; -class cir_linkedlist -{ - static class Node - { +import java.util.*; + +class CircularLinkedListQueue { + private static class Node { int data; Node link; + + Node(int data) { + this.data = data; + this.link = null; + } } - static class Queue - { + + private static class Queue { Node front, rear; + + Queue() { + front = rear = null; + } } - // Function to create Circular queue - static void enQueue(Queue q, int value) - { - Node temp = new Node(); - temp.data = value; - if (q.front == null) + + // Function to create Circular queue + static void enQueue(Queue q, int value) { + Node temp = new Node(value); + if (q.front == null) { q.front = temp; - else + } else { q.rear.link = temp; - + } q.rear = temp; - q.rear.link = q.front; + q.rear.link = q.front; // Link the last node to the front } - // Function to delete element from Circular Queue - static int deQueue(Queue q) - { - if (q.front == null) - { - System.out.printf("Queue is empty"); - return Integer.MIN_VALUE; - } - - // If this is the last node to be deleted - int value; // Value to be dequeued - if (q.front == q.rear) - { - value = q.front.data; - q.front = null; - q.rear = null; + + // Function to delete element from Circular Queue + static int deQueue(Queue q) { + if (q.front == null) { + System.out.println("Queue is empty"); + return -1; // Indicate that the queue is empty } - else // There are more than one nodes - { - Node temp = q.front; - value = temp.data; + + int value = q.front.data; + if (q.front == q.rear) { + // Only one node in the queue + q.front = q.rear = null; + } else { + // More than one node q.front = q.front.link; q.rear.link = q.front; } - + return value; } - + // Function displaying the elements of Circular Queue - static void displayQueue(Queue q) - { + static void displayQueue(Queue q) { + if (q.front == null) { + System.out.println("Queue is empty"); + return; + } + Node temp = q.front; - System.out.printf("\nElements in Circular Queue are: "); - while (temp.link != q.front) - { - System.out.printf("%d ", temp.data); + System.out.print("Elements in Circular Queue are: "); + do { + System.out.print(temp.data + " "); temp = temp.link; - } - System.out.printf("%d", temp.data); + } while (temp != q.front); + System.out.println(); } - - /* Driver of the program */ - public static void main(String args[]) - { - // Create a queue and initialize front and rear + + // Driver of the program + public static void main(String[] args) { Queue q = new Queue(); - q.front = q.rear = null; - + // Inserting elements in Circular Queue enQueue(q, 14); enQueue(q, 22); enQueue(q, 6); - + // Display elements present in Circular Queue displayQueue(q); - + // Deleting elements from Circular Queue - System.out.printf("\nDeleted value = %d", deQueue(q)); - System.out.printf("\nDeleted value = %d", deQueue(q)); - + System.out.printf("Deleted value = %d%n", deQueue(q)); + System.out.printf("Deleted value = %d%n", deQueue(q)); + // Remaining elements in Circular Queue displayQueue(q); - + enQueue(q, 9); enQueue(q, 20); displayQueue(q);