Java链接列表" insertAfter" - 方法

时间:2015-11-28 15:30:20

标签: java

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(如果是)找到Node,以下节点(在新节点之后)正在消失.. 我很确定问题是我没有设置"之前"插入后 我对实施链接列表非常缺乏经验。 我希望你能帮帮我:)

顺便理解我的代码:参数" newPunkte"意味着" newPoints"。

1 个答案:

答案 0 :(得分:2)

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

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