Author | Message |
---|---|
knagrec2
Posts: 51
|
Posted 14:28 Feb 16, 2019 |
When I am trying to test for cycles, my code would require that when the item I am trying to get has an index greater than the size, it is allowed to continue and go to the next item it has set for "node.next". This way, even if there are x elements in the set, if there is an element in the set linked to itself, it can iterate for more than x times. Is it ok to make this minor edit to the existing code? Or should I look for another way to implement Floyd's algorithm? |
Winston423
Posts: 9
|
Posted 15:42 Feb 16, 2019 |
Did you check if your loop is correctly looping to create a cycle? If it is then the a solution i used is to have one node set as a stable unchanging number and then have another node continuously check the next node after it, ex. node1 = node; (leave this alone) then node = node.next() (this is how the code we were given moves on to one another) using whatever loop your currently using and as each time it loops you constantly check if the node.next() equals node1. Because your issue is that your code constantly goes to the next node, the moment it goes back to its original node (node1) then it should stop |
knagrec2
Posts: 51
|
Posted 15:47 Feb 16, 2019 |
That's a good idea, mine was working using one node moving at a pace of one node per loop with the other at a pace of two nodes per loop. Having one be stationary should take care of the jumping out of bounds issue and solve my problem. Thanks |
Winston423
Posts: 9
|
Posted 16:02 Feb 16, 2019 |
No problem, if there is still an issue let me know. |
knagrec2
Posts: 51
|
Posted 17:11 Feb 16, 2019 |
Hello, |
Winston423
Posts: 9
|
Posted 17:26 Feb 16, 2019 |
The stationary node does not need to be a part of the cycle because it contains the same element as the 1st node (or any node u choose, doesnt matter), and since we are using booleans, if we create an if statement checking to see if stationary position 1 is equal to any one of the nodes cycling over and over (the code we are given has a getElement()), it can return true and the boolean method should stop there. |