一、更新ALGraph
(一)、Java代码
package graph;
public class ALGraph {
public VexNode[] vexs;
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;
ArcNode nextNode;
}
class VexNode {
String data;
ArcNode firstArc;
}
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);
}
}
public ALGraph(String[] vertexs, Edge[] edges) {
vexs = new VexNode[vertexs.length];
arcs = new ArcNode[edges.length];
vexNum = vertexs.length;
arcNum = edges.length;
for (int i = 0; i < vexs.length; i++) {
vexs[i] = new VexNode();
vexs[i].data = vertexs[i];
vexs[i].firstArc = null;
}
for (int i = 0; i < edges.length; i++) {
String start = edges[i].start;
String end = edges[i].end;
int indexOfStart = indexOfVex(start);
int indexOfEnd = indexOfVex(end);
ArcNode arcNode = new ArcNode();
arcNode.index = indexOfEnd;
arcNode.info = edges[i].info;
linkedLast(indexOfStart, arcNode);
arcs[i] = arcNode;
}
}
public int indexOfVex(String v) {
for (int i = 0; i < vexs.length; i++) {
if (vexs[i].data == v)
return i;
}
return -1;
}
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;
}
}
public void printALGraphByChar() {
System.out.println("邻接表存储的图:");
for (int i = 0; i < vexs.length; i++) {
System.out.print(vexs[i].data + "----->");
if (vexs[i].firstArc != null) {
ArcNode tempNode = vexs[i].firstArc;
while (tempNode.nextNode != null) {
System.out.print(vexs[tempNode.index].data + "--->");
tempNode = tempNode.nextNode;
}
System.out.print(vexs[tempNode.index].data);
} else {
System.out.print("NULL");
}
System.out.println();
}
}
public void printALGraphByIndex() {
System.out.println("邻接表存储的图:");
for (int i =