diff --git a/Marathons/Marathon.cs b/Marathons/Marathon.cs index b298e78..773da47 100644 --- a/Marathons/Marathon.cs +++ b/Marathons/Marathon.cs @@ -30,12 +30,29 @@ public sealed class Marathon Node middleNode = new Node(participant); Node? leftNode = GetNodeByIndex(insertIndex - 1); Node? rightNode = GetNodeByIndex(insertIndex); - if (leftNode == null || rightNode == null) + if (leftNode == null && rightNode == null) { _head = middleNode; _tail = middleNode; return; } + if(leftNode == null && rightNode != null) + { + _head = middleNode; + MoveNode(middleNode, rightNode); + return; + } + if(rightNode == null && leftNode != null) + { + _tail = middleNode; + MoveNode(leftNode, middleNode); + return; + } + + if(rightNode == null || leftNode == null) + { + return; + } MoveNode(leftNode, rightNode, middleNode); } @@ -100,14 +117,14 @@ public sealed class Marathon //check if the participant is the first or last int result = _head.Data?.CompareTo(participant) ?? 0; // If the participant is the first or can be placed before the first - if (result <= 0) + if (result >= 0) { exactPosFound = result == 0; return 0; } // If the participant is the last or can be placed after the last result = _tail.Data?.CompareTo(participant) ?? 0; - if (result >= 0) + if (result <= 0) { exactPosFound = result == 0; return ParticipantCount; diff --git a/Marathons/Participant.cs b/Marathons/Participant.cs index 72866ff..2cf30c8 100644 --- a/Marathons/Participant.cs +++ b/Marathons/Participant.cs @@ -24,7 +24,7 @@ public sealed class Participant // Check if the completion time is the same if(CompletionTime == participant.CompletionTime) { - return StartNo < participant.StartNo ? -1 : 1; + return StartNo > participant.StartNo ? -1 : 1; } return CompletionTime < participant.CompletionTime ? -1 : 1;