Skip to content

Commit 2bbe6a3

Browse files
authored
Create SelfLoopLinkedList.c div-bargali#185
Create SelfLoopLinkedList.c
2 parents 2ab51d7 + 1e71752 commit 2bbe6a3

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
/* Link list node */
5+
struct Node {
6+
int data;
7+
struct Node* next;
8+
};
9+
10+
/* Function to remove loop. Used by detectAndRemoveLoop() */
11+
void removeLoop(struct Node*, struct Node*);
12+
13+
/* This function detects and removes loop in the list
14+
If loop was there in the list then it returns 1,
15+
otherwise returns 0 */
16+
int detectAndRemoveLoop(struct Node* list)
17+
{
18+
struct Node *slow_p = list, *fast_p = list;
19+
20+
while (slow_p && fast_p && fast_p->next) {
21+
slow_p = slow_p->next;
22+
fast_p = fast_p->next->next;
23+
24+
/* If slow_p and fast_p meet at some point then there
25+
is a loop */
26+
if (slow_p == fast_p) {
27+
removeLoop(slow_p, list);
28+
29+
/* Return 1 to indicate that loop is found */
30+
return 1;
31+
}
32+
}
33+
34+
/* Return 0 to indeciate that ther is no loop*/
35+
return 0;
36+
}
37+
38+
/* Function to remove loop.
39+
loop_node --> Pointer to one of the loop nodes
40+
head --> Pointer to the start node of the linked list */
41+
void removeLoop(struct Node* loop_node, struct Node* head)
42+
{
43+
struct Node* ptr1;
44+
struct Node* ptr2;
45+
46+
/* Set a pointer to the beginning of the Linked List and
47+
move it one by one to find the first node which is
48+
part of the Linked List */
49+
ptr1 = head;
50+
while (1) {
51+
/* Now start a pointer from loop_node and check if it ever
52+
reaches ptr2 */
53+
ptr2 = loop_node;
54+
while (ptr2->next != loop_node && ptr2->next != ptr1)
55+
ptr2 = ptr2->next;
56+
57+
/* If ptr2 reahced ptr1 then there is a loop. So break the
58+
loop */
59+
if (ptr2->next == ptr1)
60+
break;
61+
62+
/* If ptr2 did't reach ptr1 then try the next node after ptr1 */
63+
ptr1 = ptr1->next;
64+
}
65+
66+
/* After the end of loop ptr2 is the last node of the loop. So
67+
make next of ptr2 as NULL */
68+
ptr2->next = NULL;
69+
}
70+
71+
/* Function to print linked list */
72+
void printList(struct Node* node)
73+
{
74+
while (node != NULL) {
75+
printf("%d ", node->data);
76+
node = node->next;
77+
}
78+
}
79+
80+
struct Node* newNode(int key)
81+
{
82+
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
83+
temp->data = key;
84+
temp->next = NULL;
85+
return temp;
86+
}
87+
88+
/* Drier program to test above function*/
89+
int main()
90+
{
91+
struct Node* head = newNode(50);
92+
head->next = newNode(20);
93+
head->next->next = newNode(15);
94+
head->next->next->next = newNode(4);
95+
head->next->next->next->next = newNode(10);
96+
97+
/* Create a loop for testing */
98+
head->next->next->next->next->next = head->next->next;
99+
100+
detectAndRemoveLoop(head);
101+
102+
printf("Linked List after removing loop \n");
103+
printList(head);
104+
return 0;
105+
}

0 commit comments

Comments
 (0)