How will u find whether a linked list has a loop or not?

Answers were Sorted based on User's Feedback



How will u find whether a linked list has a loop or not?..

Answer / sivaraj

BOOL findloop(struct node *start)
{
struct node *ptr,*ptr1;
ptr=start;
ptr1= start->next;
while(ptr!=NULL && ptr->next!=null && ptr1->next!=null &&
ptr1->next->next!=null)
{
if(ptr==ptr1)
return FALSE;
ptr=ptr->next;
ptr1=ptr1->next->next;
}
return TRUE;
}

Is This Answer Correct ?    16 Yes 5 No

How will u find whether a linked list has a loop or not?..

Answer / sanyam

the approach is to mark the nodes starting from the head.and
follow the links.
in the process if you find a node already marked then there
is loop.

Is This Answer Correct ?    13 Yes 2 No

How will u find whether a linked list has a loop or not?..

Answer / adi

answer 2 is correct

Is This Answer Correct ?    4 Yes 0 No

How will u find whether a linked list has a loop or not?..

Answer / priya

same logic as 3rd answer..

typedef struct node
{ int data,mark; /*mark is all initialised to 0*/
node *next;
}node;

node* findloop(node* start)
{
node *p=start;
while(p)
{
if(p->mark==0)
{
p->mark=1;
p=p->next;
}
else
{
return p;
}
}
return p;
}

Is This Answer Correct ?    3 Yes 3 No

How will u find whether a linked list has a loop or not?..

Answer / karthik

I guess the return shud b TRUE in the if(ptr==ptr1) block
since that's the oly condition for presence of a loop in the
LL.. and return shud b a false after d while loop!!!

Is This Answer Correct ?    0 Yes 0 No

How will u find whether a linked list has a loop or not?..

Answer / zameer arif

You are right. In the loop, return should be true. outside
the loop, false.

Is This Answer Correct ?    0 Yes 0 No

How will u find whether a linked list has a loop or not?..

Answer / anurag srivastava

void lopp( node **start)
{
node *temp,*newnode;
temp=*start;
while(*temp && !(temp->next==start))
{ temp=temp->next; }
if(!(*temp)) printf("Non loop");
else printf("Loop");
}

Is This Answer Correct ?    3 Yes 5 No

How will u find whether a linked list has a loop or not?..

Answer / s.v.prasad reddy

findloop(struct node *start)
{
struct node *ptr,*ptr1;
start=ptr;

while(ptr!=NULL)
{
ptr1=ptr->next;

while(ptr1!=NULL)
{
if(ptr1->link==ptr->link)
{
printf("\nLoop found at %d node",ptr->data);
exit(0);
}
ptr1=ptr1->link;
}
ptr=ptr->link;
}
printf("\nNo Loop found in list");
}

Is This Answer Correct ?    9 Yes 17 No

Post New Answer

More C Code Interview Questions

what is the output of the below program & why ? #include<stdio.h> void main() { int a=10,b=20,c=30; printf("%d",scanf("%d%d%d",&a,&b,&c)); }

6 Answers   CSC, IIIT,


main() { int i = 3; for (;i++=0;) printf(ā€œ%dā€,i); }

1 Answers   CSC,


main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }

2 Answers  


main() { printf("\nab"); printf("\bsi"); printf("\rha"); }

3 Answers  


write a c program to Create a mail account by taking the username, password, confirm password, secret_question, secret_answer and phone number. Allow users to register, login and reset password(based on secret question). Display the user accounts and their details .

2 Answers  






Write a program using one dimensional array to assign values and then display it on the screen. Use the formula a[i]=i*10 to assign value to an element.

1 Answers   Samar State University,


Program to Delete an element from a doubly linked list.

4 Answers   College School Exams Tests, Infosys,


Write a program to receive an integer and find its octal equivalent?

7 Answers  


struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); }

1 Answers  


C program to print magic square of order n where n > 3 and n is odd

2 Answers   Accenture,


write a program in c to merge two array

2 Answers  


#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }

1 Answers  


Categories