I was trying to implement Floyd's cycle finding algorithm to find loops in linked list. I implemented my code using 2 pointers but I am not able to figure out why my output is always is 0 even though a loop exists in the code.
Any suggestions? Can you find my bug?
Your addNode function is especially and unnecessarily complex.
Basically, if I'm not mistaken, the conditional test for equality is inside the while loop that only allows inequalities to get in. So you will always have a return of false, except in the case that n1->next == n1 evaluates as true. If your loop occurs somewhere else, you'll still get that there is no loop.
That's just on a glance, though, so I may be missing something.
I'm not used to looking at C++ code presently. I know that you could do pretty much exactly what you've done here in C with about 1/4 the lines, maybe less, and that's kind of a big deal. Concise code, as long as it's understandable, will help you debug.
Excuse me. You'll also get a true value on n1->next->next == n1 being evaluated as true. I'm not sure why you put the list advancements there, or why you did them the way you did.
I don't know if this is what you wanted to have happen, but your algorithm isn't sustainable the way it's written. You'll need to make a few modifications if you want to actually prove that the list is cyclical on anything of significant size. Say size of 10. At that point you wouldn't go around far enough to actually run across the first pointer, and so your program wouldn't be able to tell that it does loop back around to head eventually.
You also need to change your addNode function significantly. Look up tail pointers.
@ciphermagi: Thanks a lot for that explanation. I tried your code but it not work either :(
I looked up online and found that I can do it with 3 pointers. I modified my code and it works with 3 pointers. I am scratching my head to see if it can be done using 2 pointers. iAny suggestion if we can implement Floyd's cycle finding algorithm using 2 pointers?
I did try to make it clear that my code (which was just basically a cleaning up of your own code) was going to work with a very, very limited case, and that it wouldn't actually work for anything else.
I see how this most recent isCyclic function could work, but it's pretty bastardized, and I don't think that it would like it very much if there was a massive loop that was possibly interior linked (multiple levels of loops, or a 3-D loop).
There are ways to implement the cycle-finding algorithm. You could probably find some of them referred to at stackexchange.
I'm going to be brutally honest with you, though. This is not a C++ problem that you're having. You're having a math problem. And you really need to study the algorithm itself, not the implementation of it, if you want to be able to do this. Once you fully understand the algorithm, and you can do the math involved, then you're going to find that (and I base this off of the level of code that I've seen you use here) the implementation with C++ will be ridiculously easy.