COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
3.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  listNode
 

Typedefs

typedef struct listNode ListNode
 

Functions

double findAverageCycleLength (ListNode *arrPtr, int n)
 

Typedef Documentation

◆ ListNode

typedef struct listNode ListNode

Definition at line 8 of file 3.h.

Function Documentation

◆ 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 // For each list, we need to find the cycle length
8 // Floyd's cycle-fnding algorithm
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 // Cycle found
17 int cycleLength = 1;
18 ListNode * cycleIncrement = singleIncrement->next;
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}
Definition 3.h:4
struct listNode * next
Definition 3.h:6

References listNode::next.

Referenced by main().

Here is the caller graph for this function: