DSA-Bootcamp-Java
DSA-Bootcamp-Java copied to clipboard
Fix: Correct CLL display and insertion logic
This commit addresses two issues in the Circularly Linked List (CLL) implementation:
-
Potential infinite loop in
display(): Thedisplay()method had a conditional check (if (node.next != null)) before advancing the node pointer. This check was redundant in a CLL and could lead to an infinite loop ifnode.nextwashead(e.g. in a single-element list). The condition has been removed, andnode = node.nextis now unconditional within the loop. -
Bug in
insert()for single-element lists: When inserting the first element into an empty list, thenode.next = headassignment was missing, meaning the list wasn't actually made circular. This caused aNullPointerExceptionwhendisplay()was called on such a list. Theinsert()method has been corrected to ensurenode.next = headis set when the first element is added.
A main method with test cases for the display() method (empty list, single-element list, multi-element list) has been added to CLL.java to verify the fixes and ensure correct behavior.