ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
How will u find whether a linked list has a loop or not?
 Question Submitted By :: Vadivel152
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How will u find whether a linked list has a loop or not?
Answer
# 1
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 ?    7 Yes 10 No
S.v.prasad Reddy
 
  Re: How will u find whether a linked list has a loop or not?
Answer
# 2
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 ?    10 Yes 5 No
Sivaraj
 
 
 
  Re: How will u find whether a linked list has a loop or not?
Answer
# 3
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 ?    8 Yes 2 No
Sanyam
 
  Re: How will u find whether a linked list has a loop or not?
Answer
# 4
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 4 No
Anurag Srivastava
 
  Re: How will u find whether a linked list has a loop or not?
Answer
# 5
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 2 No
Priya
 
  Re: How will u find whether a linked list has a loop or not?
Answer
# 6
answer 2 is correct
 
Is This Answer Correct ?    1 Yes 0 No
Adi
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution) Microsoft5
main() { int a[10]; printf("%d",*a+1-*a+3); }  1
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }  1
void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }  1
main() { char *p = “ayqm”; printf(“%c”,++*(p++)); } TCS2
What are segment and offset addresses? Infosys1
main() { 41printf("%p",main); }8  1
Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. Microsoft4
main() { char a[4]="HELL"; printf("%s",a); } Wipro1
You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.  3
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18. Infosys6
Is the following code legal? struct a { int x; struct a *b; }  1
void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }  1
write a program to count the number the same (letter/character foreg: 's') in a given sentence.  1
Program to find the largest sum of contiguous integers in the array. O(n)  7
programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h  1
How will u find whether a linked list has a loop or not? Microsoft6
main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above HCL1
main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above HCL1
main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user HCL1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com