Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 63 additions & 33 deletions Data-Structures/Linked-Lists/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
Loading