Floyd’s Cycle-Finding Algorithm:
This is the fastest method. Traverse linked list using two pointers. Move one pointer by one and other pointer by two. If these pointers meet at some node then there is a loop. If pointers do not meet then linked list doesn’t have loop.
Solution:
/*
problem decription:detect loop in a linked list
method's name:floyd's cycle detection
solved by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//node structure
struct node
{
int data;
struct node *next;
}*head,*tail,*temp;
void push()//function to push a node in the linked list
{
temp=(struct node*)malloc(sizeof(struct node));
if(!(temp))
{
cout<<"memory not available!\n";
return;
}
cout<<"Enter the data\n";
cin>>temp->data;
temp->next=NULL;
if(head==NULL)//very first node marked as head
{
head=temp;
tail=temp;
}
else//for subsequent nodes,tail pointer is moved
{
tail->next=temp;
tail=temp;
}
}
bool detect_loop()//function to detect a loop
{
//floyd's cycle algo implementation
bool flag=false;
node *slowptr,*fastptr;
slowptr=head;
fastptr=head;
while(slowptr&&fastptr&&fastptr->next)
{
slowptr=slowptr->next;
fastptr=fastptr->next;
fastptr=fastptr->next;
if(slowptr==fastptr)
{
flag=true;
break;
}
}
return flag;
}
int main()
{
char flag;
while(1)
{
cout<<"Do you wanna push...\n";
cin>>flag;
switch(flag)
{
case 'y':
case 'Y':
push();
break;
case 'n':
case 'N':
break;
default:
cout<<"Wrong Choice!\nEnter your choice again\n\n\n";
}
if(flag=='n'||flag=='N')
break;
}
//creating circular linked list
if(head!=NULL)
{
tail->next=head;
}
//detecting the loop
if(detect_loop())
cout<<"There is a loop\n";
else
cout<<"No loop detcted\n";
return 0;
}
This is the fastest method. Traverse linked list using two pointers. Move one pointer by one and other pointer by two. If these pointers meet at some node then there is a loop. If pointers do not meet then linked list doesn’t have loop.
Solution:
/*
problem decription:detect loop in a linked list
method's name:floyd's cycle detection
solved by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//node structure
struct node
{
int data;
struct node *next;
}*head,*tail,*temp;
void push()//function to push a node in the linked list
{
temp=(struct node*)malloc(sizeof(struct node));
if(!(temp))
{
cout<<"memory not available!\n";
return;
}
cout<<"Enter the data\n";
cin>>temp->data;
temp->next=NULL;
if(head==NULL)//very first node marked as head
{
head=temp;
tail=temp;
}
else//for subsequent nodes,tail pointer is moved
{
tail->next=temp;
tail=temp;
}
}
bool detect_loop()//function to detect a loop
{
//floyd's cycle algo implementation
bool flag=false;
node *slowptr,*fastptr;
slowptr=head;
fastptr=head;
while(slowptr&&fastptr&&fastptr->next)
{
slowptr=slowptr->next;
fastptr=fastptr->next;
fastptr=fastptr->next;
if(slowptr==fastptr)
{
flag=true;
break;
}
}
return flag;
}
int main()
{
char flag;
while(1)
{
cout<<"Do you wanna push...\n";
cin>>flag;
switch(flag)
{
case 'y':
case 'Y':
push();
break;
case 'n':
case 'N':
break;
default:
cout<<"Wrong Choice!\nEnter your choice again\n\n\n";
}
if(flag=='n'||flag=='N')
break;
}
//creating circular linked list
if(head!=NULL)
{
tail->next=head;
}
//detecting the loop
if(detect_loop())
cout<<"There is a loop\n";
else
cout<<"No loop detcted\n";
return 0;
}
No comments:
Post a Comment