File tree 3 files changed +61
-12
lines changed
main/java/com/thealgorithms/datastructures/lists
test/java/com/thealgorithms/datastructures/lists
3 files changed +61
-12
lines changed Original file line number Diff line number Diff line change 829
829
* [ LeftistHeapTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java )
830
830
* lists
831
831
* [ 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 )
832
833
* [ CreateAndDetectLoopTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java )
833
834
* [ CursorLinkedListTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CursorLinkedListTest.java )
834
835
* [ MergeKSortedLinkedListTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedListTest.java )
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .datastructures .lists ;
2
2
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
+ */
3
7
public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
4
8
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
-
13
9
/**
14
- * Calculate the count of the list manually using recursion .
10
+ * Recursively calculates the number of nodes in the list .
15
11
*
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 .
18
14
*/
19
15
private int countRecursion (Node head ) {
20
16
return head == null ? 0 : 1 + countRecursion (head .next );
21
17
}
22
18
23
19
/**
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.
25
24
*/
26
25
@ Override
27
26
public int count () {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments