2016-04-02 29 views
1

Ödevimi tamamlamaya çalışıyorum, ancak birden fazla şubeyle bağlantılı bir listeyi nasıl oluşturacağımı bilmiyorum. Verilerimi çıkardım, daralttım ve bir Listede sakladım. city1Name ve city2Name:C# Birden çok dallı bağlantılı liste

List<Route> routes = new List<Route>(); 

Rota iki dize değişkenleri içeriyor.

Route route = new Route("FirstCity", "SecondCity"); 

Bu FirstCity ve SecondCity arasında bir rota var demektir. Her şehrin diğer şehirlere birden çok yolu olabilir.

Birisi bana bu verileri bağlantılı bir listede nasıl saklayacağımı gösterir misiniz? Bağlantılı bir listenin ne olduğunu anlıyorum ve daha sonra foreach kullanarak birden fazla olası yol verisini alabildiğimi düşünüyorum, ancak bunun için bir algoritma yazamadım. :(

+0

demo çalışma bulabilirsiniz Rota sınıfının şu tanımı

class Route { public string From { get; set; } public string To { get; set; } public Route(string from, string to) { From = from; To = to; } } 

supposing ediyorum. com/en-us/library/ms379574 (v = vs80) .aspx – InferOn

+0

@Osvaldon cevabımı aşağıya bakın, bir şehirden diğerine olası rotayı bulmak için bir snippet ekledim. Ama bu en kısa yolu bulamıyor. Burada demo bakın https://repl.it/CBgX/3 –

cevap

4

Sen T herhangi .NET uyumlu veri türü olabilir iken tip T herhangi bir öğeyi eklemek için List<T>.Add kullanabilirsiniz. Senin durumunda TRoute olduğunu. Yani, Route

örtük olarak dönüştürülebilir olabilir herhangi bir değer ekleyebilirsiniz .NET List<T> yılında Dahası
routes.Add(route); 

, bir bağlantı listesi değildir. List<T> is implemented using Array internally. NET'te bağlantı listesi uygulamasıdır LinkList<T>

DÜZENLEME

Bu yol, bir şehirden diğerine yol bulmanın çok basit bir uygulamasıdır.

static bool TryFindPath(List<Route> routes, string from, string to, int maxDepth) { 

    if (maxDepth <= 0) // To prevent StackOverFlowException 
     return false; 

    // Find all the routes with starting point == `from` 
    var startingPoints = Routes.Where(r => r.From == from).ToArray(); 
    if (startingPoints.Length == 0) // No such route exists 
     return false; 

    // See if any of route directly leads to `to` 
    var matchingRoute = startingPoints.Where(r => r.To == to).FirstOrDefault(); 
    if (matchingRoute != null) { 
     routes.Add(matchingRoute); // If so, we found that 
     return true; 
    } 


    // We are stepping into next level, decrease maxDepth by one 
    maxDepth -= 1; 

    // Otherwise iterate through all starting points and find path from 
    // that specific city refered by to our destination 
    foreach (var route in startingPoints) { 

     // Clone `routes` 
     var thisRoutes = new List<Route>(routes); 
     thisRoutes.Add(route); 

     if (TryFindPath(thisRoutes, route.To, to, maxDepth)) { 

      // Copy all newly added routes in `thisRoutes` to original `routes` 
      for (var i = routes.Count; i < thisRoutes.Count; i++) { 
       routes.Add(thisRoutes[i]); 
      } 

      return true; 
     } 
    } 

    return false; 
} 

Ben Sen bir düğüm grafiğinde https://msdn.microsoft aradığınız here

+0

Yaptığınız iş için minnettarım ve bunun için size itibar katacağım. Ama aslında ihtiyacım olan şey, bir dizi List route = yeni List (); Çünkü bu, asıl gerekliliktir :(Tek yönlü bağlantılı bir listenin neye benzediğine dair bir örnek ekleyeceğim. – Osvaldon

0
List<Route> routes = new List<Route>(); 
Route route = new Route("FirstCity", "SecondCity"); 
routes.Add(route); 

foreach (oneRoute in routes) 
{ 
    // The other work you're interested in doing 
    // oneRoute represents each single route one at a time. 
} 
İlgili konular