赞
踩
package graph; /** * 邻接表 * * Adjacency List Graph * * @author 己千之 * @time 2022/2/16(周三) */ public class ALGraph { /** * @vexs * 顶点数组 */ public VexNode[] vexs; /** * @arcs * 边数组 */ ArcNode[] arcs; /** * 顶点,边个数 */ int vexNum, arcNum; /** * 边的信息结点,如权值 */ static class Info { String value; int weight; public Info(String value) { this.value = value; } public Info(int weight) { this.weight = weight; } public Info(String value, int weight) { this.value = value; this.weight = weight; } public String toString() { return this.value + "=" + this.weight; } } /** * 弧结点,表结点 */ class ArcNode { int index; Info info; // 把int改为String ArcNode nextNode; } /** * 顶点,表头结点 */ class VexNode { String data; ArcNode firstArc; } /** * 边关系 * * @static:测试方法中,用到了Edge数组 Edge是个内部类,你要用,只能: 1.把Edge作为参数,可以用 2.把Edge设为静态调用 */ static class Edge { String start; String end; Info info; public Edge(String start, String end, Info info) { this.start = start; this.end = end; this.info = new Info(info.value, info.weight); } } /** * 构造方法,构造有向图 * * @param vertex * @param edge */ public ALGraph(String[] vertexs, Edge[] edges) { // TODO // 1.设置成员变量大小 vexs = new VexNode[vertexs.length]; arcs = new ArcNode[edges.length]; vexNum = vertexs.length; arcNum = edges.length; // 2.初始化数组 for (int i = 0; i < vexs.length; i++) { vexs[i] = new VexNode(); vexs[i].data = vertexs[i]; vexs[i].firstArc = null; } // 3.链接 for (int i = 0; i < edges.length; i++) { // 3.1 由new Edge('A', 'B', 10)来链接 String start = edges[i].start; String end = edges[i].end; int indexOfStart = indexOfVex(start); int indexOfEnd = indexOfVex(end); // 3.2 构造一个新的arcNode类型的结点 ArcNode arcNode = new ArcNode(); // one: index arcNode.index = indexOfEnd; // two: info arcNode.info = edges[i].info; // TODO ? // three: nextNode linkedLast(indexOfStart, arcNode); // 尾插法,有向,不是无向 // 3.3 更新arcs arcs[i] = arcNode; } } // stucture /** * 获取顶点元素位置 * * @param v * @return */ public int indexOfVex(String v) { // 定位顶点的位置 for (int i = 0; i < vexs.length; i++) { if (vexs[i].data == v) return i; } return -1; } // indexOfVex /** * 尾插法,将新元素插在链表尾部 * * @param index * @param node */ public void linkedLast(int indexofstart, ArcNode arcnode) { if (vexs[indexofstart].firstArc == null) { // 关键代码 vexs[indexofstart].firstArc = arcnode; } else { ArcNode tempNode = vexs[indexofstart].firstArc; while (tempNode.nextNode != null) { tempNode = tempNode.nextNode; } // 关键代码 tempNode.nextNode = arcnode; } } // linkedLast /** * 像邻接表一样打印 */ public void printALGraphByChar() { System.out.println("邻接表存储的图:"); for (int i = 0; i < vexs.length; i++) { // 1.打印顶点 System.out.print(vexs[i].data + "----->"); // 2.打印挂接链表 if (vexs[i].firstArc != null) { // 2.1 firstArc ArcNode tempNode = vexs[i].firstArc; // 2.2 nextArc while (tempNode.nextNode != null) { System.out.print(vexs[tempNode.index].data + "--->"); tempNode = tempNode.nextNode; } // 2.2 tempNode.nextNode = null System.out.print(vexs[tempNode.index].data); } else { System.out.print("NULL"); } // 3. 换行 System.out.println(); } } // printALGraph pu
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。