将 sortedInsert 插入到具有属性 Java 的单个 LinkedList.of 对象时出现问题

Trouble with a sortedInsert into a singly LinkedList.of Objects with attributes Java

此方法将创建一个新的 Node 对象,并按 CSV 中的第三个值(即 KYZ98765、ABC12345 等)的顺序插入它,除了最后一个节点插入外,它的工作原理...

这是我的代码:

void sortedInsert(Node new_node)
{
     Node current;

    /* Locate the correct node before inserting. */

    //System.out.println(current.data);
    if(head.next == null)
    {
        new_node.next = head;
        head = new_node;
        System.out.println("Inserted head: \n" + head.data.toString());
    }else
    {
        current = head;

        while (current.data.getbLnum().compareTo(new_node.data.getbLnum())>=0)
        {
            System.out.println("here again");  
            current = current.next;
        }
        new_node.next = current.next;
        current.next = new_node;
        System.out.println("Inserted: \n" + current.next.data.toString());
    }

 }

这是输出:

FundManagerLicense FundManagerLastname FundManagerFirstname ABC12345 鲍勃鲍勃 FundManagerLicense FundManagerLastname FundManagerFirstname ZYK98765 弗格森热苏斯 FundManagerLicense FundManagerLastname FundManagerFirstname GYZ98765 弗格森何塞 FundManagerLicense FundManagerLastname FundManagerFirstname KYZ98765 弗格森吉米

这是我按照每个元素馈送到 sortedInsert 的顺序输入的:

BROKER,ADD,KYZ98765,Jimmi,Ferguson,321-131,0.02

BROKER,ADD,ABC12345,Bob,Wonch,321-112,0.1

BROKER,ADD,GYZ98765,Jose,Ferguson,321-111,0.02

BROKER,ADD,ZYK98765,Jesus,Ferguson,321-141,0.02

谁能看出问题出在哪里?

你有很大的空指针异常风险。

while (current.data.getbLnum().compareTo(new_node.data.getbLnum())>=0)
        {
            System.out.println("here again");  
            current = current.next;
        }

当您到达列表末尾时,current 将变为 null,下一次调用 current.data 将抛出异常。

你知道为什么不被抛出吗?因为你把你的列表变成了循环列表!

new_node.next = head;
    head = new_node;

新节点是头节点,下一个指向自己。所以你最后一次插入一直到列表的末尾,找不到插入的地方(因为你最后一次输入应该放在列表的末尾)然后成为头部之后的下一个。

要解决这个问题,您需要进行两个更改:

void sortedInsert(Node new_node){
 Node current;

/* Locate the correct node before inserting. */

//System.out.println(current.data);
if(head.next == null)
{
    new_node.next = **null**;
    head = new_node;
    System.out.println("Inserted head: \n" + head.data.toString());
}else
{
    current = head;

    while (current.data.getbLnum().compareTo(new_node.data.getbLnum())>=0 **&& current.next != null**)
    {
        System.out.println("here again");  
        current = current.next;
    }
    new_node.next = current.next;
    current.next = new_node;
    System.out.println("Inserted: \n" + current.next.data.toString());
}
}

测试它们并告诉我。

这样试试。

private int compare(Node n1, Node n2) {
    return n1.data.getbLnum().compareTo(n2.data.getbLnum());
}

void sortedInsert(Node newNode) {
    Node prev = null;
    Node next = head;

    while (next != null &&
            compare(newNode, next) >= 0) {
        prev = next;
        next = next.next;
    }

    if (prev == null) head = newNode;
    else prev.next = newNode;
    newNode.next = next;
}

这应该是不言自明的,遍历列表,跟踪将出现在插入节点之前 (prev) 和之后 (next) 的节点,直到 next 为空,或者直到 newNode 不能出现在 next 之后。然后插入新节点,如有必要,将其设为 head.