首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >insertAfter链接列表-方法

insertAfter链接列表-方法
EN

Stack Overflow用户
提问于 2015-11-28 15:30:19
回答 1查看 5.8K关注 0票数 2
代码语言:javascript
复制
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;

    }
}

嘿,伙计们,我的代码有点问题。当我在找到节点后插入一个新节点(如果是的话),下面的节点(新节点之后)正在消失。我很确定问题是我没有在插入后设置“以前的”。我在实现链接列表方面非常缺乏经验。我希望你能帮我:)

顺便提一下,为了理解我的代码:参数"newPunkte“的意思是"newPoints”。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-28 15:35:02

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

代码语言:javascript
复制
ListNode next = current.getNext();
current.setNext(newNode);
newNode.setNext(next);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33973052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档