469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

How to check the linked list is circular or not?

please anyone tell me whether the following code works for checking the
linked list is circular or not?
Expand|Select|Wrap|Line Numbers
  1.    address 0        address1       address2
  2. | A|address1|     |B|address2|   |C|address0|
  3.    |                                          |
  4.    |                                          |
  5.   ptr1                                      ptr2
  6.  
  7.   A,B,C-are elements in list.
  8.   ptr1- points to first elt in list.
  9.   ptr2-points to last elt in list.
  10.  
  11.     if(ptr2+1 == ptr1)          
  12.     {
  13.        the list is circular.
  14.     }
Jun 4 '07 #1
12 15365
RedSon
5,000 Expert 4TB
I'm going to have to go with... no.
Jun 4 '07 #2
RedSon
5,000 Expert 4TB
The best way to figure out if a linked list is circular is to draw out your data structure on a piece of paper and pretend you are a computer. How can *you* tell if a list is circular?

Try drawing one that is circular and pretend you are a computer. Then draw one that is not circular and pretend you are a computer.

Usually if your last element points to your first element then it is circular. If you want to figure out if you have cycles then that is a complete separate issue.
Jun 4 '07 #3
Silent1Mezzo
208 100+
please anyone tell me whether the following code works for checking the
linked list is circular or not?
Expand|Select|Wrap|Line Numbers
  1.    address 0        address1       address2
  2. | A|address1|     |B|address2|   |C|address0|
  3.    |                                          |
  4.    |                                          |
  5.   ptr1                                      ptr2
  6.  
  7.   A,B,C-are elements in list.
  8.   ptr1- points to first elt in list.
  9.   ptr2-points to last elt in list.
  10.  
  11.     if(ptr2+1 == ptr1)          
  12.     {
  13.        the list is circular.
  14.     }

The reason this doesn't work is because pointers are just addresses to the place where each node is located in memory.

So lets say the address of ptr1 is 0x12BC and the address of ptr2 is 0xB901. Even though they could be linked together their addresses are totally different (so the ptr1+1 will not work).

If its a small list you could store all of the pointers and then compare each pointer against the next node's pointer. This would be very tedious (lots of loops) and not very efficient but it would do the job.
Jun 4 '07 #4
Savage
1,764 Expert 1GB
The reason this doesn't work is because pointers are just addresses to the place where each node is located in memory.

So lets say the address of ptr1 is 0x12BC and the address of ptr2 is 0xB901. Even though they could be linked together their addresses are totally different (so the ptr1+1 will not work).

If its a small list you could store all of the pointers and then compare each pointer against the next node's pointer. This would be very tedious (lots of loops) and not very efficient but it would do the job.
And boring too.

One thing is sure OP needs to dereferance the pointers and test values that they realy point to,so:

if(ptr2->next==*ptr),should be a way more closer to the solution.

Savage
Jun 4 '07 #5
RedSon
5,000 Expert 4TB
And boring too.

One thing is sure OP needs to dereferance the pointers and test values that they realy point to,so:

if(ptr2->next==*ptr),should be a way more closer to the solution.

Savage
Right but the question remains is what the OP wants is to see if the list is circular (ie. tail->next == head) or if there is a cycle (ie. node->next == visited node).

Telling if a list is circular is easy.

if (last_node->next == first_node) then its circular.
Jun 4 '07 #6
kky2k
34
Right but the question remains is what the OP wants is to see if the list is circular (ie. tail->next == head) or if there is a cycle (ie. node->next == visited node).

Telling if a list is circular is easy.

if (last_node->next == first_node) then its circular.

keep two pointers ptr1,ptr2
Expand|Select|Wrap|Line Numbers
  1.  
  2. ptr1(pointer)  increments by one step  ptr1 = HeadOfNode ;
  3. ptr2(pointer) increments by 2 steps     ptr2 = HeadOfNode -> NEXT;
  4.  
  5. while(ptr1 != NULL) //until it reaches EOL
  6. {
  7.  
  8.   if(pt1 == ptr2)  //if it is circular then at some point both the pointer will be same
  9. {
  10.     printf("VOILA ITS CIRCULAR OR HAVING LOOP");
  11.     return 0;
  12. }
  13.  else
  14. {
  15.  
  16. ptr1++;
  17. ptr2++;
  18.  
  19. }
  20. }
  21.  
Jun 5 '07 #7
RedSon
5,000 Expert 4TB
keep two pointers ptr1,ptr2
Expand|Select|Wrap|Line Numbers
  1.  
  2. ptr1(pointer)  increments by one step  ptr1 = HeadOfNode ;
  3. ptr2(pointer) increments by 2 steps     ptr2 = HeadOfNode -> NEXT;
  4.  
  5. while(ptr1 != NULL) //until it reaches EOL
  6. {
  7.  
  8.   if(pt1 == ptr2)  //if it is circular then at some point both the pointer will be same
  9. {
  10.     printf("VOILA ITS CIRCULAR OR HAVING LOOP");
  11.     return 0;
  12. }
  13.  else
  14. {
  15.  
  16. ptr1++;
  17. ptr2++;
  18.  
  19. }
  20. }
  21.  
First off, thats not going to check if there is a cycle in your linked list because a cycle can occur anywhere not just in adjacent nodes. Second, its not going to check if the linked list is circular because at no point to you check to see if the last node points to the first node. Third, incrementing a pointer is not the same as following the link to the next node. Fourth, giving code (wrong or not) is not allowed for homework questions.
Jun 5 '07 #8
Here the problem is list need not be circular such that last node point to the first node. List can be circular if last node point to any node in the list. Any other node except the last node can't make it circular, because node structure have only one next pointer. Also, here we don't know the last pointer, so can't do as lastPointer->next == firstPointer then circular.
I can think of two solutions in this case:
1) use of array which will contain the address of each node, scan the array for each node->next address, whether it's there in the array or not. if its there then list is circular and if the node->next reaches to NULL then list is not circular. Here problem is complexity of both time(O(n^2)) and of space.
2) Better solution is use of map if you are using C++(I don't know the equivalent in Java). Map will return false when key appears twice and over here key will be address of nodes.
Psudocode:
bool Fun(Struct Node* head){
Struct Node* temp=head;
while(temp!=NULL)
{
if( map.insert(temp,1) == false)
return true; //list is circular
temp=temp->next;
}
return false; //list is not circular
}

//I am not checking the code, might have some small mistake, take it as pseudocode
Jun 11 '07 #9
please anyone tell me whether the following code works for checking the
linked list is circular or not?
Expand|Select|Wrap|Line Numbers
  1.    address 0        address1       address2
  2. | A|address1|     |B|address2|   |C|address0|
  3.    |                                          |
  4.    |                                          |
  5.   ptr1                                      ptr2
  6.  
  7.   A,B,C-are elements in list.
  8.   ptr1- points to first elt in list.
  9.   ptr2-points to last elt in list.
  10.  
  11.     if(ptr2+1 == ptr1)          
  12.     {
  13.        the list is circular.
  14.     }
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.
Jun 11 '07 #10
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.

This will work only if two consecutive nodes are making list circular. if 7th node's->next is 3rd node it will not work
Jun 12 '07 #11
One more cool idea: take two pointers temp1 and temp2
initially
temp1 = head;
temp2 = head->next->next;

while(temp1!=Null && temp2!=Null)
{
if(temp1 == temp2)
return true; //list is circular
temp1=temp1->next;
temp2=temp2->next->next;
}
return false; //list is not circular

It is somewhat difficult to get it from here, but try using this algorithm on paper ..... take 8 nodes, make it circular by conecting last node with 3rd node.
Since list is circular temp2 will move to third node from 8th node. In this case temp1 == temp2 at node 5.
Complexity of this is also very less i.e. O(2n). ~ O(n)
Also, easy to implement.
If you are not clear, please get back
Jun 12 '07 #12
prat78
1
take a pointer on the linked list and keep incrementing this it by 1.
take the same pointer on the linked list and keep incrementing this by 2. This pointer will traverse at twice the speed and would meet the pointer which is been incremented by 1. is they meet this at one point this is a circular linked list.this same idea can be extended to find the centre point of a linked list(not necessarily circular)
Oct 23 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by surender | last post: by
19 posts views Thread by RAJASEKHAR KONDABALA | last post: by
5 posts views Thread by John N. | last post: by
9 posts views Thread by Goh, Yong Kwang | last post: by
14 posts views Thread by serge calderara | last post: by
23 posts views Thread by Himanshu Chauhan | last post: by
12 posts views Thread by kalyan | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.