Skip to content

Commit c766c5e

Browse files
authored
Enhance docs, remove main, add tests in `CountSinglyLinke… (#5992)
1 parent ed35374 commit c766c5e

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@
829829
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
830830
* lists
831831
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
832+
* [CountSinglyLinkedListRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursionTest.java)
832833
* [CreateAndDetectLoopTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java)
833834
* [CursorLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CursorLinkedListTest.java)
834835
* [MergeKSortedLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedListTest.java)

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
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+
*/
37
public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
48

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-
139
/**
14-
* Calculate the count of the list manually using recursion.
10+
* Recursively calculates the number of nodes in the list.
1511
*
16-
* @param head head of the list.
17-
* @return count of the list.
12+
* @param head the head node of the list segment being counted.
13+
* @return the count of nodes from the given head node onward.
1814
*/
1915
private int countRecursion(Node head) {
2016
return head == null ? 0 : 1 + countRecursion(head.next);
2117
}
2218

2319
/**
24-
* Returns the count of the list.
20+
* Returns the total number of nodes in the list by invoking the recursive
21+
* count helper method.
22+
*
23+
* @return the total node count in the list.
2524
*/
2625
@Override
2726
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)