从 LinkedList 库更改为自定义 class

Change from LinkedList library to custom class

所以我想做的是找到一种方法来更改此程序的确切功能,但使用我的自定义 LinkedList class 而不是 Java LinkedList 库。因此,我没有导入 LinkedList,而是使用了我制作的 class。问题是我在实现这个时遇到了很多麻烦。我想知道是否有关于如何执行此操作或任何解决方案的提示?

提前致谢。

主要:

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(我的自定义 class 我想使用而不是导入 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 实例,您需要:

  1. 从导入中删除 java.util.LinkedList

  2. 将您的 class LinkedList1 添加为具有完全限定 class 名称的导入,例如:xyz.abc.LinkedList1

  3. 将声明:LinkedList<Integer> list[]替换为LinkedList1 list[],并将初始化:list[i] = new LinkedList<>()替换为list[i] = new LinkedList1()

  4. 将您在 LinkedList 中使用的方法替换为 LinkedList1 中的等效方法。

如果这就是您要找的,请告诉我。