mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-15 01:40:49 +08:00
Enhance docs, remove main
, add tests in `CountSinglyLinke… (#5992)
This commit is contained in:
@ -829,6 +829,7 @@
|
|||||||
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
|
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
|
||||||
* lists
|
* lists
|
||||||
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
|
* [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)
|
* [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)
|
* [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)
|
* [MergeKSortedLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedListTest.java)
|
||||||
|
@ -1,27 +1,26 @@
|
|||||||
package com.thealgorithms.datastructures.lists;
|
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 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.
|
* @param head the head node of the list segment being counted.
|
||||||
* @return count of the list.
|
* @return the count of nodes from the given head node onward.
|
||||||
*/
|
*/
|
||||||
private int countRecursion(Node head) {
|
private int countRecursion(Node head) {
|
||||||
return head == null ? 0 : 1 + countRecursion(head.next);
|
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
|
@Override
|
||||||
public int count() {
|
public int count() {
|
||||||
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user