简体   繁体   English

尝试将节点添加到链接列表

[英]Trying to add nodes to linked list

We've started with linked list and nodes in class and I think I understood how they work. 我们已经在课堂上开始使用链表和节点,我想我理解它们是如何工作的。 But at one part, where we have to add the node to the linked list, I'm having some issues to get it to work. 但是在一个部分,我们必须将节点添加到链表中,我有一些问题要让它工作。 The mistake occurs in the method "append". 错误发生在方法“append”中。 My IDE doesn't tell me much about the issue. 我的IDE没有告诉我很多关于这个问题的信息。

My class LinkedList: 我的类LinkedList:

public class LinkedList {
    public Node head = null;

    public void insert(Node n) {
        n.next = head;
        head = n;
    }

    public Node search(int nummer) {
        Node current = head;

        while (current != null) {
            if (current.element == nummer)
                return current;
            current = current.next;
        }
        return null;
    }

    public int count() {
        int c = 0;

        for (Node n = head; n != null; n = n.next) {
            c++;
        }
        return c;
    }

    public void append(Node n) {
        if (head == null){
            head = new Node(n, null);
        }
        else {
            Node p = head;
            while (p.a != null){
                p = (Node) p.a;
            }
        p.a = new Node(n, null);}
    }    
}

My Node-class: 我的节点类:

public class Node {
    public int element = 0;
    public Node next = null;
    Object a;

    public Node(int e, Node n) {
    this.element = e;
    this.next = n;
    }

    public Node(int e) {
    this.element = e;
    }

}

Your are treating the field a as if it were the pointer to the next node in the list. 您正在将字段a视为指向列表中下一个节点的指针。 It is not, it is actually the data contained in each list node. 它不是,它实际上是每个列表节点中包含的数据 Instead, modify your append() method to use next : 相反,修改你的append()方法以使用next

public void append(Node n) {
    if (head == null) {
        head = new Node(n, null);
    }
    else {
        Node p = head;
        // walk down the list from the head until reaching the end
        while (p.next != null) {
            p = (Node) p.next;
        }
        // then append the new Node to the end of the list
        p.next = n;
    }
}

Note that ideally you should have getter and setter methods for your Node class. 请注意,理想情况下,您应该为Node类使用getter和setter方法。 I might suggest something like this: 我可能会建议这样的事情:

public class Node {
    private int element = 0;   // no idea what this is for
    private Node next = null;
    private Object a;

    public Node(int e, Node n) {
        this.element = e;
        this.next = n;
    }

    public Node(int e) {
        this.element = e;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node getNext() {
        return next;
    }

    public void setA(Object a) {
        this.a = a;
    }

    public Object getA() {
        return a;
    }
}

Using this updated definiton of Node your append() method would become: 使用这个更新的Node你的append()方法将成为:

public void append(Node n) {
    if (head == null) {
        head = new Node(n, null);
    }
    else {
        Node p = head;
        // walk down the list from the head until reaching the end
        while (p.getNext() != null) {
            p = p.getNext();
        }
        // then append the new Node to the end of the list
        p.setNext(n);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM