首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinkedList NullPointerException (需要帮助实现LinkedList)

LinkedList NullPointerException (需要帮助实现LinkedList)
EN

Stack Overflow用户
提问于 2014-12-03 21:32:20
回答 2查看 88关注 0票数 0

我需要实现一个LinkedList,到目前为止,我已经编写了我的方法,以便按照列表中的顺序插入值。我有我的Node,前台,作为我的类的实例数据,当我创建我的第一个值,并试图在我的新节点上设置前沿的下一个值时,我会得到一个nullpointerexception。

代码语言:javascript
复制
public class Class1 {
private Node front = null;//Linked List pointer
private int comparisons = 0; //# of comparisons in Lists
//private LinkedList<Node> orderLink = new LinkedList<Node>();
public Class1() {

}

/*public Class1(int x){

}*/

public void insert (int num){
    Node current = null;
    Node previous = null;
    boolean placed = false;
    if (front == null){ //generate first node of list
        Node first = new Node(num, null);
        front.setNext(first);
        placed = true;
    }
    previous = front;
    current = front.getNext();
    Node step = new Node(num, null);
    if (placed == false){
    do{
        if (step.getData() < current.getData() && step.getData() > previous.getData() || step.getData() == current.getData() || step.getData() == previous.getData()){ //if the new data is between previous and current, place. If equals current, place.
            //Insert into List
            step.setNext(current);
            previous.setNext(step);
            placed = true;
        }
        if (previous == front && step.getData() < current.getData()){ //separate case for first node
            step.setNext(current);
            previous.setNext(step);
            placed = true;
        }
        if (current.getNext() == null && step.getData() > current.getData()){ //case for last node
            current.setNext(step);
            placed = true;
        }
        //move a space up the list
        previous = current;
        current = current.getNext();

    }while(previous.getNext() != null || placed == false);
    }

}

public int search(int num){
    int nodeIndex = 0;

    return 1; //Return index of num
}
public void delete (int num){
    //delete num from the list
}
public void traverse(){
    System.out.println(front.getNext());
    System.out.println(front.getNext().getNext());
    System.out.println(front.getNext().getNext().getNext());
    System.out.println(front.getNext().getNext().getNext().getNext());
    System.out.println(front.getNext().getNext().getNext().getNext().getNext());
}
public int getComparisons(){
    return comparisons;
}

public class Node {
    private Node next;
    private int data;
    public Node(){
        next = null;
    }
    public Node(int data, Node next){
        this.next = next;
        this.data = data;
    }
    public Node getNext(){
        return next;
    }
    public void setNext(Node nextNode){
        next = nextNode;
    }
    public int getData(){
        return data;
    }
    public void setData(int data){
        this.data = data;
    }
}

}

代码语言:javascript
复制
public class User {
public static void main(String[] args){
    Class1 test = new Class1();
    test.insert(1);
    test.insert(3);
    test.insert(5);
    test.insert(2);
    test.insert(4);

    test.traverse();
}

}

我刚刚开始学习LinkedLists,我不知道我实现错了什么。任何帮助都将不胜感激。您只需要看看我的insert方法和内部Node类。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-03 21:35:39

你在这里有问题:

代码语言:javascript
复制
front.setNext(first);

您不能对null对象进行任何操作(在前面为null的情况下)。也许这里应该是:

代码语言:javascript
复制
front = first;
票数 2
EN

Stack Overflow用户

发布于 2017-01-30 01:47:02

LinkedList使用三个参数-> (leftNode、rightNode、data)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27282065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档