Skip to content

Commit 403649d

Browse files
Update CreateAndDetectLoop with tests (#5561)
1 parent b54cc21 commit 403649d

File tree

2 files changed

+115
-71
lines changed

2 files changed

+115
-71
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
package com.thealgorithms.datastructures.lists;
22

3-
import java.util.Scanner;
4-
53
public final class CreateAndDetectLoop {
4+
5+
// Node class representing a single node in the linked list
66
private CreateAndDetectLoop() {
7+
throw new UnsupportedOperationException("Utility class");
78
}
9+
static final class Node {
10+
int data;
11+
Node next;
812

9-
/**
10-
* Prints the linked list.
11-
*
12-
* @param head head node of the linked list
13-
*/
14-
static void printList(Node head) {
15-
Node cur = head;
16-
17-
while (cur != null) {
18-
System.out.print(cur.value + " ");
19-
cur = cur.next;
13+
Node(int data) {
14+
this.data = data;
15+
next = null;
2016
}
2117
}
2218

23-
/**
24-
* Creates a loop in the linked list.
25-
*
26-
* @see
27-
* <a href="https://www.geeksforgeeks.org/make-loop-k-th-position-linked-list/">
28-
* GeeksForGeeks: Make a loop at K-th position</a>
29-
* @param head head node of the linked list
30-
* @param k position of node where loop is to be created
19+
// Method to create a loop between two specific positions in the linked list
20+
/*
21+
* Test case that shows the Cycle(loop) in a LinkedList
22+
* Let's take this linked list:
23+
* 1->2->3->4->5->6
24+
* \______/
25+
* In this linked list we can see there is a cycle.
26+
* we can create loop by calling createLoop function in main after creating LL
27+
* createLoop(head,2,5);
28+
* to detect there is loop or not we can call detectloop function in main
29+
* detectloop(head);
3130
*/
32-
static void createLoop(Node head, int k) {
33-
if (head == null) {
31+
32+
static void createLoop(Node head, int position1, int position2) {
33+
if (position1 == 0 || position2 == 0) {
3434
return;
3535
}
36-
Node temp = head;
37-
int count = 1;
38-
while (count < k) { // Traverse the list till the kth node
39-
temp = temp.next;
40-
count++;
41-
}
4236

43-
Node connectedPoint = temp;
37+
Node node1 = head;
38+
Node node2 = head;
4439

45-
while (temp.next != null) { // Traverse remaining nodes
46-
temp = temp.next;
40+
int count1 = 1;
41+
int count2 = 1;
42+
// Traverse to find node at position1
43+
while (count1 < position1 && node1 != null) {
44+
node1 = node1.next;
45+
count1++;
4746
}
4847

49-
temp.next = connectedPoint; // Connect last node to k-th element
50-
}
48+
// Traverse to find node at position2
49+
while (count2 < position2 && node2 != null) {
50+
node2 = node2.next;
51+
count2++;
52+
}
5153

54+
// Create a loop by connecting node2's next to node1
55+
if (node1 != null && node2 != null) {
56+
node2.next = node1;
57+
}
58+
}
59+
// Method to detect a loop in the linked list
5260
/**
5361
* Detects the presence of a loop in the linked list.
5462
*
55-
* @see
56-
* <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">
57-
* Floyd's Cycle Detection Algorithm</a>
58-
*
59-
* @param head the head node of the linked list
60-
*
63+
* @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">Floyd's Cycle Detection Algorithm</a>
6164
* @return true if loop exists else false
6265
*/
6366
static boolean detectLoop(Node head) {
@@ -67,40 +70,10 @@ static boolean detectLoop(Node head) {
6770
while (fptr != null && fptr.next != null) {
6871
sptr = sptr.next;
6972
fptr = fptr.next.next;
70-
if (fptr == sptr) {
73+
if (sptr == fptr) {
7174
return true;
7275
}
7376
}
74-
7577
return false;
7678
}
77-
78-
public static void main(String[] args) {
79-
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
80-
Scanner sc = new Scanner(System.in);
81-
82-
System.out.println("Enter the number of elements to be inserted: ");
83-
int n = sc.nextInt();
84-
System.out.printf("Enter the %d elements: %n", n);
85-
while (n-- > 0) {
86-
singlyLinkedList.insert(sc.nextInt());
87-
}
88-
89-
System.out.print("Given list: ");
90-
printList(singlyLinkedList.getHead());
91-
System.out.println();
92-
93-
System.out.println("Enter the location to generate loop: ");
94-
int k = sc.nextInt();
95-
96-
createLoop(singlyLinkedList.getHead(), k);
97-
98-
if (detectLoop(singlyLinkedList.getHead())) {
99-
System.out.println("Loop found");
100-
} else {
101-
System.out.println("No loop found");
102-
}
103-
104-
sc.close();
105-
}
10679
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class CreateAndDetectLoopTest {
10+
11+
private CreateAndDetectLoop.Node head;
12+
13+
@BeforeEach
14+
void setUp() {
15+
// Create a linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6
16+
head = new CreateAndDetectLoop.Node(1);
17+
CreateAndDetectLoop.Node second = new CreateAndDetectLoop.Node(2);
18+
CreateAndDetectLoop.Node third = new CreateAndDetectLoop.Node(3);
19+
CreateAndDetectLoop.Node fourth = new CreateAndDetectLoop.Node(4);
20+
CreateAndDetectLoop.Node fifth = new CreateAndDetectLoop.Node(5);
21+
CreateAndDetectLoop.Node sixth = new CreateAndDetectLoop.Node(6);
22+
23+
head.next = second;
24+
second.next = third;
25+
third.next = fourth;
26+
fourth.next = fifth;
27+
fifth.next = sixth;
28+
}
29+
30+
@Test
31+
void testDetectLoopNoLoop() {
32+
// Test when no loop exists
33+
assertFalse(CreateAndDetectLoop.detectLoop(head), "There should be no loop.");
34+
}
35+
36+
@Test
37+
void testCreateAndDetectLoopLoopExists() {
38+
// Create a loop between position 2 (node with value 2) and position 5 (node with value 5)
39+
CreateAndDetectLoop.createLoop(head, 2, 5);
40+
41+
// Now test if the loop is detected
42+
assertTrue(CreateAndDetectLoop.detectLoop(head), "A loop should be detected.");
43+
}
44+
45+
@Test
46+
void testCreateLoopInvalidPosition() {
47+
// Create loop with invalid positions
48+
CreateAndDetectLoop.createLoop(head, 0, 0);
49+
50+
// Ensure no loop was created
51+
assertFalse(CreateAndDetectLoop.detectLoop(head), "There should be no loop with invalid positions.");
52+
}
53+
54+
@Test
55+
void testCreateLoopSelfLoop() {
56+
// Create a self-loop at position 3 (node with value 3)
57+
CreateAndDetectLoop.createLoop(head, 3, 3);
58+
59+
// Test if the self-loop is detected
60+
assertTrue(CreateAndDetectLoop.detectLoop(head), "A self-loop should be detected.");
61+
}
62+
63+
@Test
64+
void testCreateLoopNoChangeForNonExistentPositions() {
65+
// Create a loop with positions that don't exist in the linked list
66+
CreateAndDetectLoop.createLoop(head, 10, 20);
67+
68+
// Ensure no loop was created
69+
assertFalse(CreateAndDetectLoop.detectLoop(head), "No loop should be created if positions are out of bounds.");
70+
}
71+
}

0 commit comments

Comments
 (0)