C# ASP.NET -> Linked List problems
Hello everyone, I am a Course 1 student in the second semester and I am struggling with Linked Lists, I won't be ashamed to say that I used ChatGPT for this task, because this type of List is new to me and I can't quite fully grasp it yet. I would like to ask for help, my code fails and I don't understand why, if anyone could help me understand / give me advice on how better I could complete my task, or help me find my problem I would really appreciate it. <3
Here is the summary of my task:
There are n (1 ≤ n ≤ 100) points of m (1 ≤ m ≤ 256) colors in a rectangular coordinate plane. By connecting points in groups of three, many triangles can be formed. The colors for which you can try to form triangles are specified in the next file. Write a program that, if possible, would find three points among the points of each color, which, when connected together, would form the largest isosceles
triangles. Calculate the perimeters of the resulting triangles.
Data:
- The text file U5a.txt contains data about the points. The colors of the points and
the coordinates (x; y – integers) (-100 ≤ x, y ≤ 100) are listed in lines. The color name can consist of
two words.
- The text file U5b.txt contains a list of colors and instructions. The colors are listed in a column, next to the color name the word "yes" means that it is possible, and the word "no" means that it is not possible.
Print the color name, coordinates, perimeter or "not available" or "not possible". Sort the colors and coordinates from which triangles were successfully formed by colors in alphabetical order and perimeters in ascending order. Sort the colors from which triangles were unsuccessfully formed by colors in alphabetical order. Implement the removal of invalid triangles (invalid coordinates are entered using the keyboard) from the list.
Thank you for reading!

8 Replies
public ListTriangles FindLargestIsoscelesTriangles(ListGroupedCoordinates groupedPoints)
{
ListTriangles triangles = new ListTriangles(); // New linked list
for (groupedPoints.Start(); groupedPoints.Exists(); groupedPoints.Next())
{
(string color, CoordinatesList points) = groupedPoints.Get();
if (points.Count() < 3) // Check minimum number of points
{
triangles.AddToRight(new Triangle(color, null, 0, "not possible"));
continue;
}
double maxPerimeter = 0;
Triangle largestTriangle = null;
// Iterate over all combinations of 3 points
for (points.Start(); points.Exists(); points.Next())
{
Coordinates p1 = points.Get();
// Create a new iterator for p2
CoordinatesList pointsForP2 = new CoordinatesList();
for (points.Start(); points.Exists(); points.Next())
{
pointsForP2.AddToRight(points.Get());
}
pointsForP2 = points; // Copy the list for p2 iteration
for (pointsForP2.Start(); pointsForP2.Exists(); pointsForP2.Next())
{
Coordinates p2 = pointsForP2.Get();
// Skip if p2 is the same as p1
if (p2 == p1)
continue;
// Create a new iterator for p3
CoordinatesList pointsForP3 = new CoordinatesList();
for (pointsForP2.Start(); pointsForP2.Exists(); pointsForP2.Next())
{
pointsForP3.AddToRight(points.Get());
}
pointsForP3 = points; // Copy the list for p3 iteration
for (pointsForP3.Start(); pointsForP3.Exists(); pointsForP3.Next())
{
Coordinates p3 = pointsForP3.Get();
// Skip if p3 is the same as p1 or p2
if (p3 == p1 || p3 == p2)
continue;
// Check if the triangle is isosceles
if (IsIsosceles(p1, p2, p3))
{
double perimeter = GetPerimeter(p1, p2, p3);
if (perimeter > maxPerimeter)
{
maxPerimeter = perimeter;
CoordinatesList triangleCoordinates = new CoordinatesList();
triangleCoordinates.AddToRight(p1);
triangleCoordinates.AddToRight(p2);
triangleCoordinates.AddToRight(p3);
largestTriangle = new Triangle(color, triangleCoordinates, perimeter, "available");
}
}
}
}
}
if (largestTriangle != null)
triangles.AddToRight(largestTriangle);
else
triangles.AddToRight(new Triangle(color, null, 0, "not possible"));
}
return triangles;
}
Well, specifically your code is failing because in your expression
Checkpoint.Link != null
Checkpoint
is null. Therefore, trying to access Link
on null is going to throw that exception.
I haven't got the time to look through the code, but you can step through the debugger and see where/why Checkpoint
isn't initialized like you expect it to be.I have fixed the problem to the point that it runs:

By using this
But the problem now is that all of the triangles are null
And do not exists
It's important to ask:
Should
Checkpoint
ever be able to be null here?
If yes, then this code is correct. We would also want to make sure Checkpoint is not null.
If no, then there is a bug elsewhere in the code. Our check depends on Checkpoint
being not null.
Based on what you are seeing, my guess is the latter - Checkpoint should be getting initialized somewhere, but it's not.You are probably right, after doing many data checks I believe that all of my data is correct, the problem probably resides within the for loops/ exists, next methods, I appreciate you helping me <3
As an aside, use $code to paste code in chat.
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/