Skip to content

Commit 2418604

Browse files
authored
Add tests for SinglyLinkedList (TheAlgorithms#3913)
1 parent a7e76c5 commit 2418604

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ public boolean detectLoop() {
5454
return false;
5555
}
5656

57+
/**
58+
* Return the node in the middle of the list
59+
* If the length of the list is even then return item number length/2
60+
* @return middle node of the list
61+
*/
62+
public Node middle() {
63+
if (head == null) {
64+
return null;
65+
}
66+
Node firstCounter = head;
67+
Node secondCounter = firstCounter.next;
68+
while (secondCounter != null && secondCounter.next != null) {
69+
firstCounter = firstCounter.next;
70+
secondCounter = secondCounter.next.next;
71+
}
72+
return firstCounter;
73+
}
74+
5775
/**
5876
* Swaps nodes of two given values a and b.
5977
*
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class SinglyLinkedListTest {
11+
12+
/**
13+
* Initialize a list with natural order values with pre-defined length
14+
* @param length
15+
* @return linked list with pre-defined number of nodes
16+
*/
17+
private SinglyLinkedList createSampleList(int length) {
18+
List<Node> nodeList = new ArrayList<>();
19+
for (int i = 1; i <= length; i++) {
20+
Node node = new Node(i);
21+
nodeList.add(node);
22+
}
23+
24+
for (int i = 0; i < length - 1; i++) {
25+
nodeList.get(i).next = nodeList.get(i+1);
26+
}
27+
28+
return new SinglyLinkedList(nodeList.get(0), length);
29+
}
30+
31+
@Test
32+
void detectLoop() {
33+
//List has cycle
34+
Node firstNode = new Node(1);
35+
Node secondNode = new Node(2);
36+
Node thirdNode = new Node(3);
37+
Node fourthNode = new Node(4);
38+
39+
firstNode.next = secondNode;
40+
secondNode.next = thirdNode;
41+
thirdNode.next = fourthNode;
42+
fourthNode.next = firstNode;
43+
44+
SinglyLinkedList listHasLoop = new SinglyLinkedList(firstNode, 4);
45+
assertTrue(listHasLoop.detectLoop());
46+
47+
SinglyLinkedList listHasNoLoop = createSampleList(5);
48+
assertFalse(listHasNoLoop.detectLoop());
49+
}
50+
51+
@Test
52+
void middle() {
53+
int oddNumberOfNode = 7;
54+
SinglyLinkedList list = createSampleList(oddNumberOfNode);
55+
assertEquals(oddNumberOfNode/2 + 1, list.middle().value);
56+
int evenNumberOfNode = 8;
57+
list = createSampleList(evenNumberOfNode);
58+
assertEquals(evenNumberOfNode/2, list.middle().value);
59+
60+
//return null if empty
61+
list = new SinglyLinkedList();
62+
assertNull(list.middle());
63+
64+
//return head if there is only one node
65+
list = createSampleList(1);
66+
assertEquals(list.getHead(), list.middle());
67+
}
68+
69+
@Test
70+
void swap() {
71+
SinglyLinkedList list = createSampleList(5);
72+
assertEquals(1, list.getHead().value);
73+
assertEquals(5, list.getNth(4));
74+
list.swapNodes(1,5);
75+
assertEquals(5, list.getHead().value);
76+
assertEquals(1, list.getNth(4));
77+
}
78+
79+
@Test
80+
void clear() {
81+
SinglyLinkedList list = createSampleList(5);
82+
assertEquals(5, list.size());
83+
list.clear();
84+
assertEquals(0, list.size());
85+
assertTrue(list.isEmpty());
86+
}
87+
88+
@Test
89+
void search() {
90+
SinglyLinkedList list = createSampleList(10);
91+
assertTrue(list.search(5));
92+
assertFalse(list.search(20));
93+
}
94+
95+
@Test
96+
void deleteNth() {
97+
SinglyLinkedList list = createSampleList(10);
98+
assertTrue(list.search(7));
99+
list.deleteNth(6); //Index 6 has value 7
100+
assertFalse(list.search(7));
101+
}
102+
}

0 commit comments

Comments
 (0)