예제 #1
0
        static void Main(string[] args)
        {
            AdjList graph = new AdjList();  //实例化邻接表
            string  Input;

            //读入边 直至读入'#'或空行
            for (; ;)
            {
                Input = Console.ReadLine();
                if (Input == "#" || Input == "")
                {
                    break;
                }
                else
                {
                    graph.Add(Input);   //添加边
                }
            }

            //输出建立的邻接表的示意图
            foreach (AdjNode n in graph.Vertices)
            {
                var temp = n;
                Console.Write(n.Content + ": ");
                while (temp.Next != null)
                {
                    Console.Write(temp.Next.Content + "/" + temp.Next.Weight + " ");
                    temp = temp.Next;
                }
                Console.WriteLine();
            }

            graph.DFS();    //深度优先搜索
            graph.BFS();    //广度优先搜索

            //读入要查找的路径
            Input = Console.ReadLine();
            string X        = Input.Split(' ')[0];            //始顶点
            string Y        = Input.Split(' ')[1];            //终顶点
            int    Distence = int.Parse(Input.Split(' ')[2]); //路径长度

            graph.FindPath(X, Y, Distence);                   //查找路径
            Console.ReadLine();
        }
 //adds node to the graph
 public void AddNode(Vertex vertex)
 {
     AdjList.Add(Vertex, new List <Vertex>());
 }