Showing posts with label Loop Detection in Linked List. Show all posts
Showing posts with label Loop Detection in Linked List. Show all posts

Find and remove loop in linked list code in cpp linux

//Remove Loop in a linked list1.
//Find the loop and pass the loop node to removeLoop function.
//assign pointer ptr1=start.
//start infinite loop.
//assign pointer ptr2=loopnode.
//in loop traverse till ptr2->next!=loopnode && ptr2->next!=ptr1
//loop is found when ptr2->next=ptr1 ; break the loop
//else increment ptr1//most important
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
//Inserting at start
void insert(struct node** ref,int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*ref);
*ref=newnode;//mind it
}
//displaying
void display(struct node *ref)
{
struct node *ptr=ref;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
//Detecting Loop
void loopDetect()
{
struct node* slow=start;
struct node* fast=start;
while(start->next !=NULL)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
printf("LoopDetected\n");
removeLoop(slow);
break;//else infinite time LoopDetected gets printed
}
}
}
void removeLoop(struct node *loopnode)
{
printf("\nRemoveLoop Called.\n");
struct node *ptr1;
struct node *ptr2;
ptr1=start;
while(1)
{
ptr2=loopnode;
while(ptr2->next != loopnode && ptr2->next!=ptr1)
{
ptr2=ptr2->next;
}
if(ptr2->next==ptr1)//mind the next
break;
else
ptr1=ptr1->next;
}
ptr2->next=NULL;
}
int main()
{
insert(&start,10);
insert(&start,20);
insert(&start,30);
insert(&start,40);
insert(&start,50);
display(start);
//create a loop
start->next->next->next->next=start->next;
//display(start);//infinite loop means loop created.
loopDetect();
display(start);//No infinite loop means No loop now.

return 0;
}
/*
50 40 30 20 10
LoopDetected
50 40 30 20
*/
--------------------------
//Remove Loop in a linked list1.

How to detect/find Loop in a Linked List in C Code

/*C Linux code for detecting Loop/cycle in as a linked list.
Take two pointers advance one by one and the other by 2 nodes.
If these pointers meet then loop is present else not.*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void insert(struct node **,int d);
void display(struct node *);
int findLoop(struct node *);

int main()
{
int result;
insert(&head,10);
insert(&head,20);
insert(&head,30);
insert(&head,40);
insert(&head,50);
insert(&head,60);
display(head);
printf("\nCreate some loop.\n");
head->next->next->next->next->next=head;
result=findLoop(head);
if(result==1)
{

printf("\nYes,Loop Present in Linked List.\n ");
}
else
{

printf("\nNo,Loop Present in Linked List.\n ");
}
printf("\n");
return 0;
}
void insert(struct node **head,int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*head);
(*head)=newnode;
}
void display(struct node *head)
{
while(head)
{
printf("%d ",head->data);
head=head->next;
}
}
int findLoop(struct node *head)
{
struct node *slow=head;
struct node *fast=head;
while((fast->next!=NULL) && (fast->next->next!=NULL))
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
//printf("\nYes,Loop Present in Linked List.\n ");
return 1;
}
}
return 0;
}
/*
When run with loop outpt is:

50 40 30 20 10
Create some loop.
Yes,Loop Present in Linked List.

When run commenting loop outpt is:
50 40 30 20 10
No,Loop Present in Linked List.
*/