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
139 changes: 108 additions & 31 deletions Data-Structures/Linked-Lists/CircularLinkedLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -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)
*/
Loading