Skip to content

Commit 5fecd2a

Browse files
committed
refactor: Enhance docs, remove main, add tests in CountSinglyLinkedListRecursion
1 parent 5246f63 commit 5fecd2a

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

src/main/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursion.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
/**
4+
* CountSinglyLinkedListRecursion extends a singly linked list to include a
5+
* recursive count method, which calculates the number of nodes in the list
6+
* using a recursive helper function.
7+
*/
38
public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
49

5-
public static void main(String[] args) {
6-
CountSinglyLinkedListRecursion list = new CountSinglyLinkedListRecursion();
7-
for (int i = 1; i <= 5; ++i) {
8-
list.insert(i);
9-
}
10-
assert list.count() == 5;
11-
}
12-
1310
/**
14-
* Calculate the count of the list manually using recursion.
11+
* Recursively calculates the number of nodes in the list.
1512
*
16-
* @param head head of the list.
17-
* @return count of the list.
13+
* @param head the head node of the list segment being counted.
14+
* @return the count of nodes from the given head node onward.
1815
*/
1916
private int countRecursion(Node head) {
2017
return head == null ? 0 : 1 + countRecursion(head.next);
2118
}
2219

2320
/**
24-
* Returns the count of the list.
21+
* Returns the total number of nodes in the list by invoking the recursive
22+
* count helper method.
23+
*
24+
* @return the total node count in the list.
2525
*/
2626
@Override
2727
public int count() {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class CountSinglyLinkedListRecursionTest {
9+
10+
private CountSinglyLinkedListRecursion list;
11+
12+
@BeforeEach
13+
public void setUp() {
14+
list = new CountSinglyLinkedListRecursion();
15+
}
16+
17+
@Test
18+
public void testCountEmptyList() {
19+
// An empty list should have a count of 0
20+
assertEquals(0, list.count(), "Count of an empty list should be 0.");
21+
}
22+
23+
@Test
24+
public void testCountSingleElementList() {
25+
// Insert a single element and check the count
26+
list.insert(1);
27+
assertEquals(1, list.count(), "Count of a single-element list should be 1.");
28+
}
29+
30+
@Test
31+
public void testCountMultipleElements() {
32+
// Insert multiple elements and check the count
33+
for (int i = 1; i <= 5; i++) {
34+
list.insert(i);
35+
}
36+
assertEquals(5, list.count(), "Count of a list with 5 elements should be 5.");
37+
}
38+
39+
@Test
40+
public void testCountWithDuplicateElements() {
41+
// Insert duplicate elements and verify the count is correct
42+
list.insert(1);
43+
list.insert(2);
44+
list.insert(2);
45+
list.insert(3);
46+
list.insert(3);
47+
assertEquals(5, list.count(), "Count of a list with duplicate elements should match total node count.");
48+
}
49+
}

0 commit comments

Comments
 (0)