遍历图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<LinkItem>();
            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<TaskModel>();

            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<LinkItem> LinkData, TaskModel queue, List<TaskModel> 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; }
        /// <summary>
        /// 1,表示同时执行任务 0表示,执行完指向一个任务得结束节点 2表示执行完执行一个任务得开始节点
        /// </summary>
        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;


        }
        /// <summary>
        /// 新的,
        /// </summary>
        /// <param name="model"></param>
        /// <param name="next"></param>
        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,数据模拟的这样一个森林砸

3,测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左左在右边

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值