【问题标题】:Why am I getting a NullPointerException when inserting at the end of my custom linked list?为什么在我的自定义链表末尾插入时会出现 NullPointerException?
【发布时间】:2016-11-15 14:18:38
【问题描述】:

这是我的 Node 对象:

public class Node<Any> {

protected Any data;
protected Node<Any> link;

public Node() {
    this.data=null;
    this.link=null;
}

public Node(Any data, Node<Any> link) {
    this.data=data;
    this.link=link;
}

public void setData(Any data) { 
    this.data=data;
}

public void setLink(Node<Any> link) {
    this.link=link;
}

public Any getData() {
    return this.data;
}

public Node<Any> getLink() {
    return this.link;
}

public String toString() {
    return "Data: "+this.data+" | Link: "+this.link;
}
}

这是我的 SingleLinkedList 对象:

public class SinglyLinkedList<Any> {
private Node<Any> head;
private Node<Any> tail;
private int size;

public SinglyLinkedList() {
    this.head=null;
    this.tail=null;
    this.size=0;
}

//overloaded constructor for array to be added here

public void insertAsHead(Any thing) {
    Node<Any> tmp=new Node<Any>(); //Create new node
    tmp.data=thing; //new_node->node.data=new_value
    tmp.link=this.head; //new_node->node.link=head
    this.head=tmp; //head=new_node
    size++;
}

public void insertAsTail(Any thing) {
    if(head==null) 
        insertAsHead(thing);
    else {
        Node<Any> tmp = new Node<Any>(); //Create new node
        tmp.data=thing; //new_node->node.data=new_value
        tmp.link=null; //new_node->node.link=null
        this.tail.link=tmp; //tail->node.link=new_node
        this.tail=tmp; //tail=new_node;
    }
    size++;
}

public void insertBefore(int i, Any thing) {

}

public void insertAfter(int i, Any thing) {

}

public void insertAt(int i, Any thing) {

}

public void deleteHead() {
    size--;
}

public void deleteTail() {
    size--;
}

public void deleteAt(int i) {
    size--;
}

public Any retrieve(int i) {
    return null;
}

public void set(int i, Any thing) {

}

public boolean isEmpty() {
    if(this.size==0)
        return true;
    else
        return false;
}

public int size() {
    return this.size;
}

public String toString() {
    String s="["+this.head.data;
    Node<Any> next = this.head.link;
    while(next!=null) {
        s+=", "+next.data;
        next=next.link;
    }
    return s+"]";
}
}

当链表包含值时,每当我使用方法“insertAsTail(x)”时,程序执行就会停止并在行this.tail.link=tmp; //tail-&gt;node.link=new_node 处出现 NullPointerException。我曾尝试使用this.tail.setLink(tmp) 路线,但发生了同样的异常。

【问题讨论】:

  • 我忘了说它还没有完成,所以请原谅代码的几个部分的空洞。
  • 这意味着this.tail 为空。当列表为空时,为什么不是null
  • 您使用insertAsHead 插入第一个元素,它从不设置尾部,因此它为空。使用一个元素,您应该拥有head == tail
  • @Jaroslaw 先生,我爱你。我没有意识到如果列表中只有一个元素,我必须将头部设置为尾部。非常感谢你!附言我对这个问题的回复率感到惊讶。

标签: java oop data-structures singly-linked-list


【解决方案1】:

您确实需要学习如何使用调试器。但是,您的问题是:

this.tail.link

this.tail 在此处为空。

【讨论】:

    【解决方案2】:

    您的代码非常难以理解。 要在最后插入,您只需要检查头部是否存在。如果没有创建一个新节点并分配为头。

    如果 head 已经存在,则需要遍历到列表末尾。然后你需要添加节点。

    直到结束我才能看到你遍历列表。

    伪代码:

    public void addAtEnd(int new_data)
    {
        Node new_node = new Node(new_data);
        if (head == null){
            head = new Node(new_data);
            return;
        }
        new_node.next = null;
        Node last = head; 
        while (last.next != null)
            last = last.next;
        last.next = new_node;
        return;
    }
    

    【讨论】:

    • 对不起。我知道这不是很容易理解,但我正在努力使逻辑结构与我的数据结构教授教给我的内容接近。我开始考虑遍历整个列表的方法。还是谢谢你,好先生/女士。
    【解决方案3】:

    看起来您的(链表)尾部从未初始化。对于作为头部的第一个元素,您需要将相同的元素表示为尾部。

    public void insertAsHead(Any thing) {
        Node<Any> tmp=new Node<Any>(); //Create new node
        tmp.data=thing; //new_node->node.data=new_value
        tmp.link=this.head; //new_node->node.link=head
        this.head=tmp; //head=new_node
        if(this.size == 0){
         this.tail=this.head;
        }
        size++;
    } 
    

    基本上当你添加第一个元素时,它是头和尾,因为它是唯一的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      相关资源
      最近更新 更多