Skip to content

Fix: Correct CLL display and insertion logic #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

krishnasrivaibhav2027
Copy link

This commit addresses two issues in the Circularly Linked List (CLL) implementation:

  1. Potential infinite loop in display(): The display() 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 if node.next was head (e.g. in a single-element list). The condition has been removed, and node = node.next is now unconditional within the loop.

  2. Bug in insert() for single-element lists: When inserting the first element into an empty list, the node.next = head assignment was missing, meaning the list wasn't actually made circular. This caused a NullPointerException when display() was called on such a list. The insert() method has been corrected to ensure node.next = head is 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.

This commit addresses two issues in the Circularly Linked List (CLL) implementation:

1.  **Potential infinite loop in `display()`:** The `display()` 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 if `node.next` was `head` (e.g. in a single-element list). The condition has been removed, and `node = node.next` is now unconditional within the loop.

2.  **Bug in `insert()` for single-element lists:** When inserting the first element into an empty list, the `node.next = head` assignment was missing, meaning the list wasn't actually made circular. This caused a `NullPointerException` when `display()` was called on such a list. The `insert()` method has been corrected to ensure `node.next = head` is 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant