public VNode[] MVexs; // 顶点数组 #endregion Fields #region Constructors /// <summary> /// 创建图(用已提供的矩阵) /// </summary> /// <param name="vexs">顶点数组</param> /// <param name="edges">边</param> public ListUdg(City[] vexs, EData[] edges) { // 初始化"顶点数"和"边数" int vlen = vexs.Length; int elen = edges.Length; // 初始化"顶点" MVexs = new VNode[vlen]; for (int i = 0; i < MVexs.Length; i++) { MVexs[i] = new VNode { Data = vexs[i], FirstEdge = null }; } // 初始化"边" MEdgNum = elen; for (int i = 0; i < elen; i++) { // 读取边的起始顶点和结束顶点 City c1 = edges[i].StartCity; City c2 = edges[i].EndCity; int weight = edges[i].Weight; // 读取边的起始顶点和结束顶点 int p1 = GetPosition(c1); int p2 = GetPosition(c2); if (p1 == -1) { Debug.WriteLine("未找到开始城市:" + c1.Name); continue;; } if (p1 == -1) { Debug.WriteLine("未找到结束城市:" + c2.Name); continue; } // 初始化node1 ENode node1 = new ENode { Ivex = p2, Weight = weight }; // 将node1链接到"p1所在链表的末尾" if (MVexs[p1].FirstEdge == null) MVexs[p1].FirstEdge = node1; else LinkLast(MVexs[p1].FirstEdge, node1); // 初始化node2 ENode node2 = new ENode { Ivex = p1, Weight = weight }; // 将node2链接到"p2所在链表的末尾" if (MVexs[p2].FirstEdge == null) MVexs[p2].FirstEdge = node2; else LinkLast(MVexs[p2].FirstEdge, node2); } }
/// <summary> /// 将node节点链接到list的最后 /// </summary> /// <param name="list"></param> /// <param name="node"></param> public void LinkLast(ENode list, ENode node) { ENode p = list; while (p.NextEdge != null) p = p.NextEdge; p.NextEdge = node; }