Witam, jestem dość nowy w C# i wciąż miewam dziwne problemy z tym językiem. Ostatnio próbowałem napisać algorytm znajdujący trasę z punktu A do B.
Oto jak wygląda sam algorytm:
private route Way(place b, route r,int i)
{
if (r.LastPlace==b)
return r;
if (r.LastPlace.Container.Count > i)
{
r.GoTo(r.LastPlace.Container[i]);
return Way(b, r, 0);
}
else
{
r.GoBack();
MessageBox.Show("fsad");
return Way(b, r, ++i);
}
}
a tak wygląda moja struktura route:
public struct route
{
//-----------------------------------------------------------------------------------------------variables
uint distance;
LinkedList places;
place start;
//-----------------------------------------------------------------------------------------------properties
public int NumeberOfPlaces
{
get { return places.Count+1; } //+1 - starting place
}
public int NumberOfRoads
{
get { return places.Count; }
}
public uint Distance
{
get { return distance; }
}
public place LastPlace
{
get
{
if (distance == 0)
return start;
else
return places.Last.Value.Destination;
}
}
public place StartingPlace
{
get { return start; }
}
//-----------------------------------------------------------------------------------------------methods
public route(place start)
{
this.start = start;
distance = 0;
places = new LinkedList();
}
public void GoTo(road p)
{
distance += p.Distance;
places.AddLast(p);
}
public void GoBack()
{
if (distance > 0)
{
distance -= places.Last.Value.Distance;
places.RemoveLast();
}
}
}
Generalnie algorytm wydaje się być względnie w porządku, bo dla krótkich tras działa bez zarzutu, ale gdy wybiorę odrobinę dłuższą (np. przez 4 miejsca), podświetla mi się
return places.Last.Value.Destination;
we właściwości LastPlace i mówi, że mam wyjątek: Cannot evaluate expression because the current thread is in a stack overflow state.} System.Exception {System.StackOverflowException}
czy coś z tym można zrobić?