int loop_detection(node_type *head)
{
 node_type *p1, *p2;
 p1=head;
 p2=head;
 while(p1 && p2)
 {

  p1=p1->next;
  if(p1==NULL)
   return FALSE;

  if(p1==p2)
  {
	return TRUE;
  }

  p1=p1->next;

  if(p1==p2)
  {
	return TRUE;
  }
  p2=p2->next;
 }
 return FALSE;
}


int loop_break(node_type *head)
{
 int count=0;
 int i;
 node_type *p1, *p2;
 p1=head;
 p2=head;
 while(p1 && p2)
 {
  p1=p1->next;
  if(p1==NULL)
   break;
  if(p1==p2)
  {
   break;
  }
  p1=p1->next;
  if(p1==p2)
  {
   break;
   //return TRUE;
  }
  p2=p2->next;
 }
 printf("Loop Detected at %d\n",p1->data);
 
 //Phase 2
 p2=p1->next;
 while(p1!=p2)
 {
  p2=p2->next;
  count++;
 }
 printf("The counter = %d\n",count);
 p1=head;
 for(i=0;inext;
 }
 p2=head;
 while(p1->next !=p2)
 {
  p1=p1->next;
  p2=p2->next;
 }
 printf("The p1->data = %d\n",p1->data);
 printf("The p2->data = %d\n",p2->data);
 p1->next=NULL;
 return FALSE;
}

Free Web Hosting