链接列表 - 找不到为什么在插入方法之前插入导致链接列表展开 [英] Linked list - Can't figure out why this insert before method is causing the link list to expand

查看:132
本文介绍了链接列表 - 找不到为什么在插入方法之前插入导致链接列表展开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不使用insertBefore方法时,它只是正常打印链接列表,就像它应该的那样。但是,当我尝试使用insertBefore方法时,它确实适用于第一部分,但它会一直打印链接列表,就像它永远持续下去一样。

例如:



没有插入之前,它会打印出我的测试:::::
头 - > 3 - > 2 - > 1 - > 4 - > | ||



但是,当我使用insertBefore并将其打印出来时,它将打印出

head - > 3 - > 4 - > 2 - > 1 - > 4 - > 2 - > 1 - > 4 - > 2 - > 1 - > 4 - > 2 - > 1 - > 4 - > 2 - > 1 - > 4 - >并且它会一直持续下去并且一直持续下去

这里是insertBefore方法

$ $ $ $ $ $ c> private boolean insertBefore(Node aNode,Node beforeNode)
{
Node currentNode;
节点prevNode;
// aNode = new Node();

currentNode = this.getHead(); (currentNode!= null&&& currentNode.getNext()!= aNode)
{

if(currentNode == beforeNode)
{
prevNode = this.getPrevious(beforeNode);
prevNode.setNext(aNode);
aNode.setNext(beforeNode);
//aNode.setNext(currentNode);
返回true;
}

currentNode = currentNode.getNext();
}
currentNode.setNext(beforeNode);

返回false;


insertBefore方法可以完成它的工作,但它会使链接列表永远持续下去,我想知道为什么

解决方案

您正在操纵看起来像双链表你有getPrevious)



您当前的列表是:
1 - 2 - 3 4 - / p

您想在3之前添加'a':



  • 因此您需要找到节点2& 3
  • 更新2.在a之后,a.之前2
  • 将a.Next更新为3和3.之前有一个

    我没有在这里看到所有这些更新..只有PrevNode.setNext(aNode)。



    注意(这不是双向链表)只缺少1个不是3个,但是为了找到前一个节点,实际上不需要再对列表进行二次全面扫描)。


    When I don't use the "insertBefore" method, it just prints out the linked list normally like it is supposed to. But when I try to use the insertBefore method, it does work for the first part but then it keeps printing the link list as if it goes on forever,

    ex:

    without insert before it prints out " my Tests::::
    head ->3 -> 2 -> 1 -> 4 -> ||| "

    But when I use insertBefore and print it out, it prints out

    head ->3 -> 4 -> 2 -> 1 -> 4 -> 2 -> 1 -> 4 -> 2 -> 1 -> 4 -> 2 -> 1 -> 4 -> 2 -> 1 -> 4 -> and it keeps on going and going forever

    here is the insertBefore method

    private boolean insertBefore(Node aNode, Node beforeNode)
    {
        Node currentNode;
        Node prevNode;
        //aNode= new Node();
    
        currentNode = this.getHead();
    
        while(currentNode!=null && currentNode.getNext()!=aNode)
        {
    
            if(currentNode == beforeNode)
                {
                    prevNode = this.getPrevious(beforeNode);
                    prevNode.setNext(aNode);
                    aNode.setNext(beforeNode);
                    //aNode.setNext(currentNode);
                    return true;
                }
    
            currentNode = currentNode.getNext();
        }
        currentNode.setNext(beforeNode);
    
        return false;
    
    }
    

    The insertBefore method does do its job, but then it makes the link list continue on forever, and i'm wondering why

    解决方案

    You are manipulating what looks like a doubly linked list (since you have getPrevious)

    Your current list is: 1 <-> 2 <-> 3 <-> 4

    You want to add 'a' before 3:

  • so you need to find Node 2 & 3
  • update 2.Next to a, and a.Previous to 2
  • update a.Next to 3 and 3.Previous to a

    I don't see all these updates here.. only PrevNode.setNext(aNode).

    Note (This is not a Doubly linked list then you are only missing 1 set not 3 but then there is really no need to do a full second scan of the list twice just to find the previous node).

    这篇关于链接列表 - 找不到为什么在插入方法之前插入导致链接列表展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆