How will u find whether a linked list has a loop or not?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
write a c-program to display the time using FOR loop
void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }
main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } }
what is the output of following program ? void main() { int i=5; printf("%d %d %d %d %d ",i++,i--,++i,--i,i); }
main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16
how to check whether a linked list is circular.
char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }
main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }
main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); }
void ( * abc( int, void ( *def) () ) ) ();
Which version do you prefer of the following two, 1) printf(“%s”,str); // or the more curt one 2) printf(str);
Is the following code legal? struct a { int x; struct a *b; }