【问题标题】:Trying to add nodes to linked list尝试将节点添加到链表
【发布时间】:2017-01-15 14:48:43
【问题描述】:

我们从课堂上的链表和节点开始,我想我理解它们是如何工作的。但一方面,我们必须将节点添加到链表中,我遇到了一些问题才能让它工作。 错误发生在“追加”方法中。我的 IDE 并没有告诉我太多关于这个问题的信息。

我的班级链表:

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);}
    }    
}

我的节点类:

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;
    }

}

【问题讨论】:

  • 您有什么问题?运行代码时会发生什么?结果与您想要的有什么不同?在您的编程生涯中,学习如何调试自己的代码非常重要。您应该使用调试器或添加System.out.println() 语句来帮助您查看代码实际在做什么。我还建议你画图来形象化你在做什么。使用方框或圆圈表示节点,使用箭头表示节点之间的next 引用。

标签: java linked-list nodes


【解决方案1】:

您将字段a 视为指向列表中下一个节点的指针。不是,实际上是每个列表节点中包含的data。相反,修改您的 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;
    }
}

请注意,理想情况下,您的Node 类应该有getter 和setter 方法。我可能会建议这样的事情:

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;
    }
}

使用 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);
    }
}

【讨论】:

  • 感谢您的更新版本,但问题仍然存在。 “新节点(n,null)中的n,现在说“节点无法转换为int”。
  • 这就是所有存在的代码,我已经多次寻找错误。
  • 发现错误,抱歉。只需使用p.setNext(n),直接追加新的Node即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多