gpt4 book ai didi

Java 链表 "insertAfter"- 方法

转载 作者:行者123 更新时间:2023-11-30 08:43:57 26 4
gpt4 key购买 nike

public void insertAfter(String after, String newName, int newPunkte)
{
ListNode newNode = new ListNode(newName, newPunkte, null);

if(head == null)
{
System.out.println("'InsertAfter' is not possible.");
return;
}
else
{
current = head;

while(current != null)
{
if(current.getName().equals(after))
{
System.out.println(after+" was found. "+newName+" was created.");
//Here is my problem...
current.setNext(newNode);
return;
}
previous = current;
current = current.getNext();
}
System.out.println(after+" was not found.");
return;

}
}

大家好,我的代码有点问题。当我在之后插入一个新节点时a (if so) found Node, following Nodes (after the new one) are disappearing..我很确定问题是我在插入后没有设置“上一个”。我对实现链表缺乏经验。我希望你能帮助我:)

顺便说一句,为了理解我的代码:参数“newPunkte”表示“newPoints”。

最佳答案

需要在newNode上设置下一个节点:

ListNode next = current.getNext();
current.setNext(newNode);
newNode.setNext(next);

关于Java 链表 "insertAfter"- 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973052/

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