please anyone tell me whether the following code works for checking the
linked list is circular or not?
-
address 0 address1 address2
-
| A|address1| |B|address2| |C|address0|
-
| |
-
| |
-
ptr1 ptr2
-
-
A,B,C-are elements in list.
-
ptr1- points to first elt in list.
-
ptr2-points to last elt in list.
-
-
if(ptr2+1 == ptr1)
-
{
-
the list is circular.
-
}
It is not as complex as You are assuming.
1. Have ptr1 node pointing to head node.
2. Have a ptr2 node pointing to next of ptr1 node.
3. within While Loop with condition ptr2 not null compare
ptr1 and ptr2, if they are equal then print as Circular and break
increment the loop counter ptr2 to next node.
Is this clear ???
Regards,
Girish.