6. Now Modify your program to remove the loop in same linklst.
/* pass the head reference to detectLoop */
int detectLoop(struct node *list)
{
struct node *slowp = list, *fastp = list;
while (slowp && fastp && fastp->next)
{
slowp = slowp->next;
fastp = fastp->next->next;
if (slow_p == fast_p)
{
printf("\r\n Loop detected in given linked list.\r\n");
removeloop(slowp, list);
return 1;
}
}
return 0;
}
void removeLoop(struct node *slow_node, struct node *head)
{
struct node *ptr1;
struct node *ptr2;
ptr1 = head;
while (1)
{
ptr2 = loop_node;
while (ptr2->next != loop_node && ptr2->next != ptr1)
ptr2 = ptr2->next;
if (ptr2->next == ptr1)
break;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
}
Ashwani
16 Aug, 2016 3:11 PM
int detectLoop(struct node *list)
{
struct node *slowp = list, *fastp = list;
while (slowp && fastp && fastp->next)
{
slowp = slowp->next;
fastp = fastp->next->next;
if (slow_p == fast_p)
{
printf("\r\n Loop detected in given linked list.\r\n");
removeloop(slowp, list);
return 1;
}
}
return 0;
}
void removeLoop(struct node *slow_node, struct node *head)
{
struct node *ptr1;
struct node *ptr2;
ptr1 = head;
while (1)
{
ptr2 = loop_node;
while (ptr2->next != loop_node && ptr2->next != ptr1)
ptr2 = ptr2->next;
if (ptr2->next == ptr1)
break;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
}
Report Error
Report Error
Please Login First Click Here