|
| 1 | +// CircularNode class to represent each element in the circular linked list |
| 2 | +class CircularNode { |
| 3 | + int data; |
| 4 | + CircularNode next; |
| 5 | + |
| 6 | + // Constructor to initialize the node with data |
| 7 | + public CircularNode(int data) { |
| 8 | + this.data = data; |
| 9 | + this.next = null; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +// CircularLinkedList class to manage the linked list operations |
| 14 | +public class CircularLinkedList { |
| 15 | + CircularNode head; |
| 16 | + |
| 17 | + // Method to add a node at the end of the list |
| 18 | + public void add(int data) { |
| 19 | + CircularNode newNode = new CircularNode(data); |
| 20 | + |
| 21 | + // If the list is empty, make the new node the head |
| 22 | + if (head == null) { |
| 23 | + head = newNode; |
| 24 | + head.next = head; // Point the node to itself |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // Traverse to the end of the list |
| 29 | + CircularNode temp = head; |
| 30 | + while (temp.next != head) { |
| 31 | + temp = temp.next; |
| 32 | + } |
| 33 | + |
| 34 | + // Add the new node at the end of the list |
| 35 | + temp.next = newNode; |
| 36 | + newNode.next = head; // Link the new node to the head |
| 37 | + } |
| 38 | + |
| 39 | + // Method to insert a node at a specific position |
| 40 | + public void insert(int data, int position) { |
| 41 | + CircularNode newNode = new CircularNode(data); |
| 42 | + |
| 43 | + // If inserting at the head |
| 44 | + if (position == 0) { |
| 45 | + if (head == null) { |
| 46 | + head = newNode; |
| 47 | + head.next = head; |
| 48 | + return; |
| 49 | + } |
| 50 | + CircularNode temp = head; |
| 51 | + while (temp.next != head) { |
| 52 | + temp = temp.next; |
| 53 | + } |
| 54 | + newNode.next = head; |
| 55 | + head = newNode; |
| 56 | + temp.next = head; |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Traverse to the position |
| 61 | + CircularNode temp = head; |
| 62 | + for (int i = 0; i < position - 1 && temp.next != head; i++) { |
| 63 | + temp = temp.next; |
| 64 | + } |
| 65 | + |
| 66 | + // Insert the new node |
| 67 | + newNode.next = temp.next; |
| 68 | + temp.next = newNode; |
| 69 | + } |
| 70 | + |
| 71 | + // Method to delete a node at a specific position |
| 72 | + public void delete(int position) { |
| 73 | + if (head == null) { |
| 74 | + System.out.println("List is empty."); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // If deleting the head node |
| 79 | + if (position == 0) { |
| 80 | + if (head.next == head) { |
| 81 | + head = null; |
| 82 | + return; |
| 83 | + } |
| 84 | + CircularNode temp = head; |
| 85 | + while (temp.next != head) { |
| 86 | + temp = temp.next; |
| 87 | + } |
| 88 | + head = head.next; |
| 89 | + temp.next = head; |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // Traverse to the position |
| 94 | + CircularNode temp = head; |
| 95 | + for (int i = 0; i < position - 1 && temp.next != head; i++) { |
| 96 | + temp = temp.next; |
| 97 | + } |
| 98 | + |
| 99 | + // Delete the node |
| 100 | + if (temp.next == head) { |
| 101 | + System.out.println("Invalid position."); |
| 102 | + return; |
| 103 | + } |
| 104 | + temp.next = temp.next.next; |
| 105 | + } |
| 106 | + |
| 107 | + // Method to display the list |
| 108 | + public void display() { |
| 109 | + if (head == null) { |
| 110 | + System.out.println("List is empty."); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + CircularNode temp = head; |
| 115 | + do { |
| 116 | + System.out.print(temp.data + " "); |
| 117 | + temp = temp.next; |
| 118 | + } while (temp != head); |
| 119 | + System.out.println(); |
| 120 | + } |
| 121 | + |
| 122 | + // Main method to test the circular linked list implementation |
| 123 | + public static void main(String[] args) { |
| 124 | + CircularLinkedList cll = new CircularLinkedList(); |
| 125 | + |
| 126 | + // Add elements to the list |
| 127 | + cll.add(10); |
| 128 | + cll.add(20); |
| 129 | + cll.add(30); |
| 130 | + cll.add(40); |
| 131 | + |
| 132 | + // Display the list |
| 133 | + System.out.println("Circular Linked List:"); |
| 134 | + cll.display(); |
| 135 | + |
| 136 | + // Insert an element at position 2 |
| 137 | + cll.insert(25, 2); |
| 138 | + System.out.println("After inserting 25 at position 2:"); |
| 139 | + cll.display(); |
| 140 | + |
| 141 | + // Delete the element at position 3 |
| 142 | + cll.delete(3); |
| 143 | + System.out.println("After deleting the element at position 3:"); |
| 144 | + cll.display(); |
| 145 | + } |
| 146 | +} |
0 commit comments