Skip to content
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

refactor: Enhance docs, remove main, add tests in `CountSinglyLinke… #5992

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
* lists
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
* [CountSinglyLinkedListRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursionTest.java)
* [CreateAndDetectLoopTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java)
* [CursorLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CursorLinkedListTest.java)
* [MergeKSortedLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedListTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package com.thealgorithms.datastructures.lists;

/**
* CountSinglyLinkedListRecursion extends a singly linked list to include a
* recursive count method, which calculates the number of nodes in the list.
*/
public class CountSinglyLinkedListRecursion extends SinglyLinkedList {

public static void main(String[] args) {
CountSinglyLinkedListRecursion list = new CountSinglyLinkedListRecursion();
for (int i = 1; i <= 5; ++i) {
list.insert(i);
}
assert list.count() == 5;
}

/**
* Calculate the count of the list manually using recursion.
* Recursively calculates the number of nodes in the list.
*
* @param head head of the list.
* @return count of the list.
* @param head the head node of the list segment being counted.
* @return the count of nodes from the given head node onward.
*/
private int countRecursion(Node head) {
return head == null ? 0 : 1 + countRecursion(head.next);
}

/**
* Returns the count of the list.
* Returns the total number of nodes in the list by invoking the recursive
* count helper method.
*
* @return the total node count in the list.
*/
@Override
public int count() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.thealgorithms.datastructures.lists;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CountSinglyLinkedListRecursionTest {

private CountSinglyLinkedListRecursion list;

@BeforeEach
public void setUp() {
list = new CountSinglyLinkedListRecursion();
}

@Test
public void testCountEmptyList() {
// An empty list should have a count of 0
assertEquals(0, list.count(), "Count of an empty list should be 0.");
}

@Test
public void testCountSingleElementList() {
// Insert a single element and check the count
list.insert(1);
assertEquals(1, list.count(), "Count of a single-element list should be 1.");
}

@Test
public void testCountMultipleElements() {
// Insert multiple elements and check the count
for (int i = 1; i <= 5; i++) {
list.insert(i);
}
assertEquals(5, list.count(), "Count of a list with 5 elements should be 5.");
}

@Test
public void testCountWithDuplicateElements() {
// Insert duplicate elements and verify the count is correct
list.insert(1);
list.insert(2);
list.insert(2);
list.insert(3);
list.insert(3);
assertEquals(5, list.count(), "Count of a list with duplicate elements should match total node count.");
}
}