GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 389 of 453    Print  
You have a link list. How can you tell whether there is a cycling in it?

  
Total Answers and Comments: 3 Last Update: January 16, 2008     Asked by: sivagangadhar1983 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 12, 2007 03:00:17   #1  
krishnakumar_hbti        

RE: You have a link list. How can you tell whether the...
To get the cycle in linked list efficiently we must have to argument the data structure by having the mark information in each node and keeping head node.

mark info of head :-will keep the values indicating the value that a each unvisited node will contain for a single traversing.( this value will toggle for alternate traversals.)


mark info of other node:- will contain the information that node is visited or not. to get cycle:- we will start with head node get the value of mark info and store in a temporary variable.
This value is the value that a node must contain if it is not visited in this traversal of list. then we toggle the mark value of head (in next traversal this value will be the test for un travesed nodes).


now we will visit rest node and check if this value is same as that of temporary variable. if (yes) then this is node that is not visited we will toggle mark info of node and visit next node. if (no) this node is already visited so there is cycle.


 
Is this answer useful? Yes | No
January 14, 2008 11:12:50   #2  
yossale Member Since: January 2008   Contribution: 1    

RE: You have a link list. How can you tell whether there is a cycling in it?
The trick is NOT to change the list (meaning you can't change the data).
The solution is this:
Hold 2 pointers and each cycle pointer A will go "jump" 2 nodes and pointer B will "jump" 1 node. If they ever meet there's a circle.
But how will you know if there isn't a circle? if there is no circle one of the pointers will end up at the end of the list...

 
Is this answer useful? Yes | No
January 16, 2008 02:38:56   #3  
santhigkinterview Member Since: January 2008   Contribution: 5    

RE: You have a link list. How can you tell whether there is a cycling in it?

Take 2 pointer. increment 1 pointer once and another pointer twice.
keep incrementing unless pointer2 is NULL or when pointer 1 and pointer 2 are equal.

When both pointer equal there is a Loop existing.
otherwise no Loop Detected.

/* Source Code */
/* Assume that there is a linked list which is already existing */

void findLoop()
{
struct llist *ptr1 *ptr2;

ptr1 head;
ptr2 head->link;

if(ptr1 ptr2)
printf("nLoop Detectedn");
else
{
while((ptr2 ! NULL) && (ptr1 ! ptr2))
{
ptr1 ptr1->link;
ptr2 ptr2->link;
if(ptr2 ! NULL)
ptr2 ptr2->link;
}
if(ptr2 NULL)
printf("Loop is not Detectedn");
else
{
if(ptr1 ptr2)
{
printf("Loop Detectedn");
}
}
}

}


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape