Friday, September 28, 2012

How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.


typedef struct node 
void *data; 
struct node *next;
}mynode; 

mynode * find_loop(NODE * head)
mynode *current = head; 
while(current->next != NULL) 
mynode *temp = head; 
while(temp->next != NULL && temp != current)
if(current->next == temp) 
printf("\nFound a loop."); 
return current; 
temp = temp->next;
current = current->next;
return NULL; 
}

No comments: