遍历图OR森林C#代码实现

1,最近写业务用到了遍历森林,知道根节点和结束节点,实现挺不容易的,给大家分享一下吧。由于我写的测试代码没啥关系,把类和业务都混到一块了,在开发项目中大家一定要分离。

using AutoTaskServer.BLL;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
       

       static  TaskModel task = null;

        static void Main(string[] args)
        {
            var today = DateTime.Now.Date;

            var list = new List();
            list.Add(new LinkItem {  source = "1", target = "2", type = "1" });       
            list.Add(new LinkItem {  source = "1", target = "3", type = "1" });
            list.Add(new LinkItem { source = "1", target = "4", type = "1" });

            list.Add(new LinkItem {  source = "2", target = "11", type = "1" });
            list.Add(new LinkItem {  source = "2", target = "12", type = "1" });
            list.Add(new LinkItem {  source = "3", target = "6", type = "1" });
            list.Add(new LinkItem {  source = "3", target = "7", type = "1" });
            list.Add(new LinkItem {  source = "3", target = "8", type = "1" });
            list.Add(new LinkItem { source = "4", target = "9", type = "1" });


            list.Add(new LinkItem {  source = "9", target = "10", type = "1" });
            list.Add(new LinkItem {  source = "9", target = "13", type = "1" });
            list.Add(new LinkItem { source = "11", target = "13", type = "1" });
            list.Add(new LinkItem { source = "12", target = "14", type = "1" });
            list.Add(new LinkItem {  source = "6", target = "15", type = "1" });
            list.Add(new LinkItem { source = "7", target = "15", type = "1" });
            list.Add(new LinkItem {  source = "8", target = "16", type = "1" });
           
            task = new TaskModel
            {
                currentId = "1",
                nextTask = null
            };
            var result = new List();

            Console.WriteLine("---------------请输入两个数字---------------");
            var s1 = Console.ReadLine();
            var s2 = Console.ReadLine();
            ComuputeQueue(s1, s2, list, task, result);
            Console.WriteLine("---------------开始计算---------------");

            foreach (var item in result)
            {
                Console.WriteLine("---------------*****---------------");
                var tmp = item;
                while (tmp != null)
                {
                    Console.WriteLine("id:" + tmp.currentId);
                    tmp = tmp.nextTask;
                }

            }
            Console.ReadKey();

        }

        private static void ComuputeQueue(string prev, string end, List LinkData, TaskModel queue, List result)
        {
            var nextList = LinkData.Where(c => c.source == prev).OrderByDescending(c => c.source);

            if (nextList.Count() > 0)
            {
                for (int x = 0; x < nextList.Count(); x++)
                {
                    queue.nextTask = new TaskModel
                        {
                            currentId = nextList.ElementAt(x).target,
                            nextTask = null
                        };
                        if (queue.nextTask.currentId != end)
                        {
                            ComuputeQueue(queue.nextTask.currentId, end, LinkData, queue.nextTask, result);
                        }
                        else
                        {
                          
                            result.Add(task.copy());
                          
                        }
                    
                 
                }
            }


        }
    }
    public class LinkItem
    {
        public string source { set; get; }

        public string target { set; get; }
        /// 
        /// 1,表示同时执行任务 0表示,执行完指向一个任务得结束节点 2表示执行完执行一个任务得开始节点
        /// 
        public string type { set; get; }

        public string id { set; get; }
    }
 public class TaskModel
    {
        public string currentId { set; get; }

        public TaskModel nextTask { set; get; }

        public TaskModel copy()
        {
            var item = new TaskModel();
            item.currentId = currentId;
            item.nextTask = null;
            if (nextTask != null)
                copyNext(item, nextTask);
            return item;


        }
        /// 
        /// 新的,
        /// 
        /// 
        /// 
        private void copyNext(TaskModel newTask, TaskModel oldTask)
        {
            newTask.nextTask = new TaskModel
            {
                currentId = oldTask.currentId,
                nextTask = null
            };
            if (oldTask.nextTask != null)
            {
                copyNext(newTask.nextTask, oldTask.nextTask);
            }
        }
    }
}

2,数据模拟的这样一个森林砸

遍历图OR森林C#代码实现_第1张图片

3,测试结果

遍历图OR森林C#代码实现_第2张图片

你可能感兴趣的:(随笔代码)