Go to the source code of this file.
◆ ListNode
Definition at line 8 of file 3.h.
◆ findAverageCycleLength()
double findAverageCycleLength |
( |
ListNode * | arrPtr, |
|
|
int | n ) |
Definition at line 4 of file 3.c.
4 {
5 unsigned int totalCycleLength = 0;
6 for (int i = 0; i < n; i++) {
7
8
9 ListNode * singleIncrement = &arrPtr[i];
10 ListNode * doubleIncrement = &arrPtr[i];
11 while (1) {
12 if (singleIncrement->
next == NULL || doubleIncrement->
next == NULL) {
13 break;
14 }
15 if (singleIncrement == doubleIncrement->
next) {
16
17 int cycleLength = 1;
19 while (cycleIncrement != singleIncrement) {
20 cycleLength++;
21 cycleIncrement = cycleIncrement->
next;
22 }
23 totalCycleLength += cycleLength;
24 break;
25 }
26 singleIncrement = singleIncrement->
next;
27 doubleIncrement = doubleIncrement->
next->
next;
28 }
29 }
30 return (double) totalCycleLength / n;
31}
References listNode::next.
Referenced by main().