EmbLogic's Blog

function to detect a loop in linked list and remove the loop.

int fix_loop(struct node **start)// pass start as a pass by reference argument
{
struct node *temp,**ma;
int count = 0,i;
temp = *start;
ma = calloc(sizeof(struct node *),20);
while(1)
{
if(!(temp->next))
{
printf(“there is no loop\n”);
return -1;
}
for(i=0; i<count; i++)
{
if(temp->next == *(ma+i))// create a master array which contains all the unique addresses
goto o;
//            printf(“i = %d\n”,i);

}
printf(”                            count = %d\n”,count);
*(ma+(count)) = temp;
count++;
//        ma = realloc(ma,count+1);
temp = temp->next;

}
o:    temp->next= NULL;
printf(“loop!! fixed\n”);
free(ma);
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>