how to check whether a linked list is circular.
Answers were Sorted based on User's Feedback
Answer / coder_1
Create two pointers, each set to the start of the list.
Update each as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next; if (pointer2)
pointer2=pointer2->next;
if (pointer1 == pointer2) {
print (\"circular\n\");
}
}
If a list is circular, at some point pointer2 will wrap
around and be either at the item just before pointer1, or
the item before that. Either way, it?s either 1 or 2 jumps
until they meet.
Is This Answer Correct ? | 86 Yes | 34 No |
Answer / pratyu
node *ptr1,*ptr2;
ptr1=head;
ptr2=head->next;
while(ptr2)
{
if(ptr1==ptr2)
printf("circular linked list");
ptr2=ptr2->next;
}
printf("not a circular linked list");
Is This Answer Correct ? | 31 Yes | 7 No |
Answer / priya
This is the function to check if the linklist is circular:
bool CLinklist::ifcircular()
{
node *ptr1,*ptr2;
ptr1 = front;
ptr2 = front;
while(ptr1)
{
ptr1 = ptr1->link;
ptr2 = ptr2->link;
if(ptr2)
ptr2 = ptr2->link;
if(ptr1 == ptr2)
return 1;
}
}
Is This Answer Correct ? | 15 Yes | 7 No |
consider home pointer as the starting pointer of the linked
list.
consider temp as the temporary pointer.
temp = home;
while(temp != NULL)
{
if(temp -> next == start)
{
flag = 1;
break;
}
else
flag = 0;
temp = temp -> next;
}
if(flag == 1)
printf("Circular");
else
printf("Not circular");
Is This Answer Correct ? | 30 Yes | 30 No |
Answer / nirwal
hi All
specialy the two pointer solution techies
what about the loop in the link list ?
how you differentiate b/w circular list and list having the
loop?
Think again...........
Is This Answer Correct ? | 4 Yes | 4 No |
Answer / abhi
while(q->next->next=NULL)
{
p=p->next;
q=q->next->next;
if(p==q)
{
printf("loof is find");
break;
}
}
Is This Answer Correct ? | 2 Yes | 2 No |
Answer / shahid khan abbasi
bool hasCircle(List l)
{
Iterator i = l.begin(), j = l.begin();
while (true) {
// increment the iterators, if either is at the end,
you're done, no circle
if (i.hasNext()) i = i.next(); else return false;
// second iterator is travelling twice as fast as first
if (j.hasNext()) j = j.next(); else return false;
if (j.hasNext()) j = j.next(); else return false;
// this should be whatever test shows that the two
// iterators are pointing at the same place
if (i.getObject() == j.getObject()) {
return true;
}
}
}
Is This Answer Correct ? | 2 Yes | 3 No |
Answer / burmeselady
start from pointer1
check all node's next pointer is null or not starting from
pointer1 until pointer1
if a node next pointer is null=> it is not circular.
else it is circular
Is This Answer Correct ? | 5 Yes | 7 No |
Answer / ishan
by checking whether link field in the last node contains
the address of the first node.
Is This Answer Correct ? | 17 Yes | 20 No |
Answer / lohitha
node *ptr1,*ptr2;
ptr1=head;
ptr2=head->next;
while(ptr2)
{
if(ptr1==ptr2)
{
printf("circular linked list");
break;
}
ptr2=ptr2->next;
}
printf("not a circular linked list");
Is This Answer Correct ? | 3 Yes | 6 No |
Write a routine that prints out a 2-D array in spiral order
void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }
main() { char str1[] = {‘s’,’o’,’m’,’e’}; char str2[] = {‘s’,’o’,’m’,’e’,’\0’}; while (strcmp(str1,str2)) printf(“Strings are not equal\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.
int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }
String reverse with time complexity of n/2 with out using temporary variable.
#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }
main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }
Derive expression for converting RGB color parameters to HSV values
int a = 10 + 10 .... ,... A = A * A What would be the value of A? The answer is 120!! Could anyone explain this to me.
2 Answers Bosch, eInfochips, HCL, IHCL,
How can you relate the function with the structure? Explain with an appropriate example.
What are segment and offset addresses?