所以我要做的是找到一种方法来更改该程序的确切功能,但使用我的自定义LinkedList类而不是Java LinkedList库。因此,我不是导入 LinkedList,而是使用我创建的类。问题是我在实现这个时遇到了很多麻烦。我想知道是否有任何有关如何执行此操作的提示或任何解决方案?
提前谢谢。
主要:
File f = new File("ass3.txt");
Scanner scan = new Scanner(f);
if (f.exists() == false) {
System.out.println("File doesn't exist or could not be found.");
System.exit(0);
}
int nVertices = scan.nextInt();
int nEdges = scan.nextInt();
for (int i = 0; i < 21; i++) {
String s = scan.nextLine();
}
int[] dong = new int[99];
Graph graph = new Graph(nVertices);
for (int i = 0; i < 99; i++) {
String vertex = scan.next();
String connected = scan.next();
int weight = scan.nextInt();
dong[i] = weight;
graph.addEdge(Graph.convertInt(vertex), Graph.convertInt(connected));
}
String startPoint = scan.next();
String finishPoint = scan.next();
graph.printGraph1(dong);
LinkedList1(我想使用我的自定义类而不是导入 LinkedList(:
static class LinkedList1 {
Node head;
static class Node {
static int data;
Node next;
Node(int d) {
data = d;
}
}
public LinkedList1 insert(LinkedList1 list, int data) {
Node new_node = new Node(data);
new_node.next = null;
if (list.head == null) {
list.head = new_node;
} else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
return list;
}
public void printList(LinkedList1 list) {
Node currNode = list.head;
System.out.print("LinkedList: ");
while (currNode != null) {
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
}
@Override
public String toString() {
return "Data: " + Node.data;
}
}
图:
static class Graph {
int vertex;
LinkedList<Integer> list[];
public Graph(int vertex) {
this.vertex = vertex;
list = new LinkedList[vertex];
for (int i = 0; i < vertex; i++) {
list[i] = new LinkedList<>();
}
}
public void addEdge(int source, int destination) {
//add edge
list[source].addFirst(destination);
//add back edge ((for undirected)
list[destination].addFirst(source);
}
public void printGraph() {
for (int i = 0; i < vertex; i++) {
if (list[i].size() > 0) {
System.out.print("Vertex " + convertString(i) + " is connected to: ");
for (int j = 0; j < list[i].size(); j++) {
System.out.print(list[i].get(j) + " ");
}
System.out.println();
}
}
}
public void printGraph1(int[] dong) {
for (int i = 0; i < vertex; i++) {
if (list[i].size() > 0) {
System.out.print("Vertex " + convertString(i) + " is connected to: ");
for (int j = 0; j < list[i].size(); j++) {
int l = list[i].get(j);
System.out.print(convertString(l) + "(" + dong[j] + ") ");
}
System.out.println();
}
}
}
static int convertInt(String convert) {
switch (convert) {
case "a":
return 0;
case "b":
return 1;
case "c":
return 2;
case "d":
return 3;
case "e":
return 4;
case "f":
return 5;
case "g":
return 6;
case "h":
return 7;
case "i":
return 8;
case "j":
return 9;
case "k":
return 10;
case "l":
return 11;
case "m":
return 12;
case "n":
return 13;
case "o":
return 14;
case "p":
return 15;
case "q":
return 16;
case "r":
return 17;
case "s":
return 18;
case "t":
return 19;
}
return 0;
}
}
static String convertString(int convert) {
switch (convert) {
case 0:
return "a";
case 1:
return "b";
case 2:
return "c";
case 3:
return "d";
case 4:
return "e";
case 5:
return "f";
case 6:
return "g";
case 7:
return "h";
case 8:
return "i";
case 9:
return "j";
case 10:
return "k";
case 11:
return "l";
case 12:
return "m";
case 13:
return "n";
case 14:
return "o";
case 15:
return "p";
case 16:
return "q";
case 17:
return "r";
case 18:
return "s";
case 19:
return "t";
}
return "";
}
如果您只是在寻找一种将现有LinkedList
实例替换为您的实例的方法,则需要:
-
从导入中删除
java.util.LinkedList
。 -
将类
LinkedList1
添加为具有完全限定类名的导入,例如:xyz.abc.LinkedList1
-
将声明:
LinkedList<Integer> list[]
替换为LinkedList1 list[]
,并将初始化:list[i] = new LinkedList<>()
替换为list[i] = new LinkedList1()
。 -
将
LinkedList
中使用的方法替换为LinkedList1
中的等效方法。
让我知道这是否是您要找的。