| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Cycle detection
I was wondering if anyone knew any good cycle detection algorithims for an array of linked lists. If anyone knows any good links or code (even pseudocode) it would help me out tons! I need an algorithim that will detect a cycle not just in a element of the array (its not possible in my situation) but the array as a whole.
for something like this... ListType *Wait_For; ÏWait_For= new ListType[Max]; |
|
#2
|
|||
|
|||
|
Hmm...not something I've particularly looked at but my thoughts would be to do it as follows.
Start at the first element of the array and mark the first element of the list. Follow its next pointer and mark that and so on. When you reach the end of a linked list go to the next element in the array and continue marking. If you reach the last element of the array and the last element of the linked list then there are no cycles. If you encounter a marked node before that then there is a cycle. No idea if this works or how well since its early but it should give you a good starting point if nothing else, -KM- |
|
#3
|
|||
|
|||
|
A simple technique for detecting infinite loops inside lists, symlinks, state machines, etc. For example, to detect a loop inside a linked list:
|
|
#4
|
|||
|
|||
|
can u please the hare and tortoise solution more clear with an example
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Cycle detection |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|