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 |
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.
main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }
main() { int i=10; i=!i>14; Printf ("i=%d",i); }
what is oop?
main() { char a[4]="HELL"; printf("%s",a); }
void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above
main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000
main() { char *p = “ayqm”; printf(“%c”,++*(p++)); }
29 Answers IBM, TCS, UGC NET, Wipro,
WAP to display 1,2,3,4,5........N
Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.
How to palindrom string in c language?