diff --git a/Data-Structures/Linked-Lists/LinkedList.java b/Data-Structures/Linked-Lists/LinkedList.java index c819a14..0e4e156 100644 --- a/Data-Structures/Linked-Lists/LinkedList.java +++ b/Data-Structures/Linked-Lists/LinkedList.java @@ -75,11 +75,11 @@ public void deleteFromEnd() { } temp.next = null; } - // ---- Delete From position ---- - public void deleteFromPosition(int position) { - if (head == null) { - System.out.println("List is Empty!"); - return; + // ---- Delete From position ---- + public void deleteFromPosition(int position) { + if (head == null) { + System.out.println("List is Empty!"); + return; } if (position < 1) { System.out.println("Invalid position!"); @@ -96,9 +96,33 @@ public void deleteFromPosition(int position) { if (temp == null || temp.next == null) { System.out.println("Position out of range!"); return; - } - temp.next = temp.next.next; - } + } + temp.next = temp.next.next; + } + // ---- Delete By Value ---- + public void deleteByValue(int key) { + if (head == null) { + System.out.println("List is Empty!"); + return; + } + if (head.data == key) { + head = head.next; + System.out.println("Deleted element: " + key); + return; + } + Node prev = head; + Node curr = head.next; + while (curr != null) { + if (curr.data == key) { + prev.next = curr.next; + System.out.println("Deleted element: " + key); + return; + } + prev = curr; + curr = curr.next; + } + System.out.println("Element " + key + " not found. No deletion performed."); + } public void search(int key) { Node temp = head; int position = 1; @@ -137,13 +161,14 @@ public static void main(String[] args) { System.out.println("3. Insert at Position"); System.out.println("4. Delete from Beginning"); System.out.println("5. Delete from End"); - System.out.println("6. Delete from Position"); - System.out.println("7. Search"); - System.out.println("8. Display"); - System.out.println("9. Exit"); - System.out.print("Enter your choice: "); - choice = sc.nextInt(); - switch (choice) { + System.out.println("6. Delete from Position"); + System.out.println("7. Delete by Value"); + System.out.println("8. Search"); + System.out.println("9. Display"); + System.out.println("10. Exit"); + System.out.print("Enter your choice: "); + choice = sc.nextInt(); + switch (choice) { case 1: System.out.print("Enter element to Insert at Beginning: "); data = sc.nextInt(); @@ -172,21 +197,26 @@ public static void main(String[] args) { position = sc.nextInt(); list.deleteFromPosition(position); break; - case 7: - System.out.print("Enter element to Search: "); - data = sc.nextInt(); - list.search(data); - break; - case 8: - list.display(); - break; - case 9: - System.out.println("Exiting..."); - break; - default: - System.out.println("Invalid choice!"); - } - } while (choice != 9); - sc.close(); - } -} + case 7: + System.out.print("Enter element value to Delete: "); + data = sc.nextInt(); + list.deleteByValue(data); + break; + case 8: + System.out.print("Enter element to Search: "); + data = sc.nextInt(); + list.search(data); + break; + case 9: + list.display(); + break; + case 10: + System.out.println("Exiting..."); + break; + default: + System.out.println("Invalid choice!"); + } + } while (choice != 10); + sc.close(); + } +}