将sortedInsert放入具有Java属性的对象的单链接列表时出现问题
此方法将创建一个新的节点对象,并按CSV中的第三个值(即KYZ98765、ABC12345等)顺序插入该对象。除了最后一个节点插入外,该方法有效 这是我的代码:将sortedInsert放入具有Java属性的对象的单链接列表时出现问题,java,sorting,singly-linked-list,Java,Sorting,Singly Linked List,此方法将创建一个新的节点对象,并按CSV中的第三个值(即KYZ98765、ABC12345等)顺序插入该对象。除了最后一个节点插入外,该方法有效 这是我的代码: void sortedInsert(Node new_node) { Node current; /* Locate the correct node before inserting. */ //System.out.println(current.data); if(head.next == n
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());
}
}
以下是输出:
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());
}
}
基金经理授权基金经理lastName基金经理firstName
ABC12345 Wonch Bob
基金经理授权基金经理lastName基金经理firstName
ZYK98765弗格森耶稣
基金经理授权基金经理lastName基金经理firstName
GYZ98765弗格森何塞
基金经理授权基金经理lastName基金经理firstName
KYZ98765 Ferguson Jimmi
这是我的输入,按照每个元素被送入sortedInsert的顺序:
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());
}
}
经纪人,地址:KYZ98765,杰米,弗格森,321-131,0.02
经纪人,地址:ABC12345,Bob,Wonch,321-112,0.1
经纪人,地址:GYZ98765,何塞,弗格森,321-111,0.02
经纪人,地址:ZYK98765,耶稣,弗格森,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的调用将引发和异常
while (current.data.getbLnum().compareTo(new_node.data.getbLnum())>=0)
{
System.out.println("here again");
current = current.next;
}
你知道为什么不被扔吗?因为你把你的名单列成了循环名单
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
之后。然后插入新节点,如有必要,将其设置为头部
您希望得到什么输出?现在它继续将所有内容放入头部,并提供一个包含1个对象的列表。