From efaecbd114d258d3b7f15a6d501027f0e9297145 Mon Sep 17 00:00:00 2001 From: Amal Bijoy Date: Tue, 7 Apr 2026 11:32:09 +0530 Subject: [PATCH] Add deleteByValue operation to circular linked list menu --- .../Linked-Lists/CircularLinkedLists.java | 139 ++++++++++++++---- 1 file changed, 108 insertions(+), 31 deletions(-) diff --git a/Data-Structures/Linked-Lists/CircularLinkedLists.java b/Data-Structures/Linked-Lists/CircularLinkedLists.java index 5d882b4..fef3a82 100644 --- a/Data-Structures/Linked-Lists/CircularLinkedLists.java +++ b/Data-Structures/Linked-Lists/CircularLinkedLists.java @@ -59,11 +59,11 @@ public void deleteFromBeginning() { } } - public void deleteFromEnd() { - if (tail == null) { - System.out.println("List is Empty. Can't Delete."); - return; - } + public void deleteFromEnd() { + if (tail == null) { + System.out.println("List is Empty. Can't Delete."); + return; + } Node head = tail.next; @@ -79,9 +79,51 @@ public void deleteFromEnd() { } System.out.println(tail.data + " Deleted from End."); - temp.next = tail.next; - tail = temp; - } + temp.next = tail.next; + tail = temp; + } + + public void deleteByValue(int key) { + if (tail == null) { + System.out.println("List is Empty. Can't Delete."); + return; + } + + Node head = tail.next; + + if (head == tail) { + if (head.data == key) { + System.out.println(key + " Deleted from List."); + tail = null; + } else { + System.out.println("Element " + key + " NOT Found!"); + } + return; + } + + Node prev = tail; + Node current = head; + do { + if (current.data == key) { + prev.next = current.next; + + if (current == tail) { + tail = prev; + } + + if (current == head) { + tail.next = current.next; + } + + System.out.println(key + " Deleted from List."); + return; + } + prev = current; + current = current.next; + } while (current != head); + + System.out.println("Element " + key + " NOT Found!"); + } public void display() { if (tail == null) { @@ -128,11 +170,12 @@ public static void main(String[] args) { System.out.println("1. Insert at Beginning"); System.out.println("2. Insert at End"); System.out.println("3. Delete from Beginning"); - System.out.println("4. Delete from End"); - System.out.println("5. Search"); - System.out.println("6. Display"); - System.out.println("7. Exit"); - System.out.print("Enter your choice: "); + System.out.println("4. Delete from End"); + System.out.println("5. Delete by Value"); + System.out.println("6. Search"); + System.out.println("7. Display"); + System.out.println("8. Exit"); + System.out.print("Enter your choice: "); choice = sc.nextInt(); @@ -157,25 +200,59 @@ public static void main(String[] args) { cll.deleteFromEnd(); break; - case 5: - System.out.print("Enter element to Search: "); - int key = sc.nextInt(); - cll.search(key); - break; - - case 6: - cll.display(); - break; - - case 7: - System.out.println("Exiting..."); - break; + case 5: + System.out.print("Enter element to Delete: "); + int deleteKey = sc.nextInt(); + cll.deleteByValue(deleteKey); + break; + + case 6: + System.out.print("Enter element to Search: "); + int key = sc.nextInt(); + cll.search(key); + break; + + case 7: + cll.display(); + break; + + case 8: + System.out.println("Exiting..."); + break; default: System.out.println("Invalid choice!"); } - } while (choice != 7); - - sc.close(); - } -} + } while (choice != 8); + + sc.close(); + } +} + +/* +Sample Output: + +--- Circular Linked List Operations Menu --- +1. Insert at Beginning +2. Insert at End +3. Delete from Beginning +4. Delete from End +5. Delete by Value +6. Search +7. Display +8. Exit +Enter your choice: 2 +Enter element to Insert at End: 10 +10 Inserted at the End. + +Enter your choice: 2 +Enter element to Insert at End: 20 +20 Inserted at the End. + +Enter your choice: 5 +Enter element to Delete: 10 +10 Deleted from List. + +Enter your choice: 7 +20 -> (head) +*/