private static IEnumerable<Course> CreateBasicUnsortedList()
 {
     var a = new Course("B", new Course("A", null));
     var b = new Course("E", new Course("C", new Course("D", null)));
     var c = new Course("F", new Course("A", null));
     return new[] { a, b, c };
 }
 private static IEnumerable<Course> CreateComplexUnsortedList()
 {
     var a = new Course("B", new Course("A", null));
     var b = new Course("E", new Course("C", new Course("D", new Course("G", null))));
     var c = new Course("F", new Course("A", null));
     var d = new Course("H", new Course("D", null));
     return new[] { a, b, c, d };
 }
Example #3
0
        private static void Visit(Course course, Func<Course, Course> getDependencies, StringBuilder sorted, Dictionary<string, bool> visited)
        {
            if (visited.ContainsKey(course.Title)) return;

            visited[course.Title] = true;
            var dependency = getDependencies(course);
            if (dependency != null) Visit(dependency, getDependencies, sorted, visited);

            if (sorted.Length == 0) sorted.Append(course.Title);
            else sorted.Append(", " + course.Title);
        }
 public static void AddCourse(this List<Course> courses, Course course)
 {
     if (courses.Count > 0)
     {
         foreach (var rootCourse in courses)
         {
             if (course.Dependency != null && rootCourse.Title == course.Dependency.Title)
             {
                 course.Dependency = rootCourse;
                 courses.Remove(rootCourse);
                 courses.Add(course);
                 return;
             }
             var courseInChain = rootCourse;
             while (courseInChain.Dependency != null)
             {
                 if (course.Dependency == null)
                 {
                     if (course.Title == courseInChain.Title || course.Title == courseInChain.Dependency.Title) return;
                 }
                 else
                 {
                     if (course.Title == courseInChain.Dependency.Title && courseInChain.Dependency.Dependency == null)
                     {
                         courseInChain.Dependency = course;
                         return;
                     }
                 }
                 courseInChain = courseInChain.Dependency;
             }
             if (course.Title == courseInChain.Title && course.Dependency != null)
             {
                 courseInChain.Dependency = new Course(course.Dependency.Title, null);
                 return;
             }
         }
     }
     courses.Add(course);
 }
 private Course[] CreateComplexCyclicList()
 {
     var a = new Course("A", new Course("C", new Course("B", new Course("A", null))));
     return new[] { a };
 }