gpt4 book ai didi

java - LinkedList NullPointerException(需要帮助实现 LinkedList)

转载 作者:行者123 更新时间:2023-12-02 05:11:43 26 4
gpt4 key购买 nike

我需要实现一个 LinkedList,到目前为止,我已经编写了在列表中按顺序插入值的方法。我有我的节点 front 作为我的类的实例数据,当创建我的第一个值并尝试将 front 的 next 值设置为指向我的新节点时,我得到了一个空指针异常。

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

}

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,我不确定我实现了什么错误。任何帮助将不胜感激。您实际上只需要查看我的插入方法和内部 Node 类。

最佳答案

您在这里遇到问题:

front.setNext(first);

您不能对 null 对象进行任何操作(并且在 front 为 null 的情况下)。大概这里应该是:

front = first;

关于java - LinkedList NullPointerException(需要帮助实现 LinkedList),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27282065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com