数据结构实验十的路径问题
先放代码(关键代码在最后,不要跳过了)
1.这是邻接表的类
package SY10;
public class GraphList{
public static class VertexList{
public String data;
public EdgeListNode firstEdege;
public double weight;
public VertexList(String data,EdgeListNode firstEdege){
this.data=data;
this.firstEdege=firstEdege;
}
}
public static class EdgeListNode{
public String data;
public int vertexIndex;
public EdgeListNode next;
public double weight;
public EdgeListNode(int vertexIndex,double weight,EdgeListNode next){
this.vertexIndex=vertexIndex;
this.weight=weight;
this.next=next;
}
public EdgeListNode(int vertexIndex,String data,double weight,EdgeListNode next){
this.vertexIndex=vertexIndex;
this.data=data;
this.weight=weight;
this.next=next;
}
}
public VertexList[] vertexes;
public int edgeCount;
public GraphList(int vertexCount,int edgeCount){
this.edgeCount=edgeCount;
this.vertexes=new VertexList[vertexCount];
}
}
2.迪杰斯特拉算法求最短路径
package SY10;
import java.util.Scanner;
import SY10.GraphList.EdgeListNode;
import SY10.GraphList.VertexList;
import SY3.SqStack;
public class SY10_Graph3 {
private static Scanner scanner;
public static void main(String[] args)throws Exception {
scanner = new Scanner(System.in);
GraphList graph=createFromConsole1();
System.out.println("智能省路系统");
System.out.println("1查询景点信息");
System.out.println("2查询两个景点路径");
switch (scanner.nextInt()) {
case 1:
System.out.println("请输入你想要查询的景点名称:");
String point=scanner.next();
for(int i=0;i<14;i++){
if(point.equals(graph.vertexes[i].data))
System.out.println(graph.vertexes[i].data);
}
break;
case 2:
int first=0;
int end=0;
System.out.println("请输入你想要查询的两个景点名称:");
System.out.println("请输入第一个:");
String point1=scanner.next();
for(int i=0;i<14;i++){
if(point1.equals(graph.vertexes[i].data))
first=i;
}
System.out.println("请输入第二个:");
String point2=scanner.next();
for(int i=0;i<14;i++){
if(point2.equals(graph.vertexes[i].data))
end=i;
}
dijkstra(graph,first,end);
break;
}
}
public static GraphList createFromConsole() {
scanner = new Scanner(System.in);
System.out.print("请输入顶点数:");
int vertexCount=scanner.nextInt();
System.out.print("请输入边数:");
int edgeCount=scanner.nextInt();
GraphList graph=new GraphList(vertexCount,edgeCount);
for(int i=0;i<vertexCount;i++){
graph.vertexes[i]=new VertexList("v"+i,new EdgeListNode