java - 从 LinkedList 库更改为自定义类

标签 java linked-list adjacency-list

所以我想做的是找到一种方法来更改该程序的确切功能,但使用我的自定义 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与您的实例一样,您需要:

  1. 删除java.util.LinkedList来自您的进口。

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

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

  4. 替换 LinkedList 中您使用过的方法使用 LinkedList1 中的等效方法.

请告诉我这是否是您正在寻找的内容。

关于java - 从 LinkedList 库更改为自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58247593/

相关文章:

java - Spring Boot - 测试用例 - 不要加载所有组件

java - 什么时候应该在变量名中使用美元符号 ($)?

c - 链表无法正确循环

javascript - 链表 - 删除最后一个节点

将一个链表复制到另一个链表中

mysql - 邻接列表模型或嵌套集模型,我应该使用哪种数据模型来存储分层数据?

big-o - Θ(deg(u)) 是什么意思?

java - 当屏幕旋转时,用户可以欺骗 Android

MySQL - 邻接表模型 - 获取深度

用于网络抓取或网络挖掘的 Java API