【问题标题】:linked list add node not working链表添加节点不起作用
【发布时间】:2012-10-26 19:27:34
【问题描述】:

我正在尝试创建一个将节点添加到我的链接列表的方法。该方法需要一个 int(指定新链接应该去哪里)和 String(因为我的链表包含字符串)。我写了一些我认为会在我的列表中的特定点添加一个链接的代码,但是当我在假设添加一个新节点之后打印我的列表时,我看到新节点尚未添加。我很惊讶,因为我在编写代码时非常小心地测试了代码的行为,并且 add 方法似乎符合我的预期——但是新打印的列表并没有反映添加新链接后的更改.谁能告诉我哪里出错了:/

ps:类和方法的名称没有争议,我的老师选择了它们,这就是它们必须保留的方式。

谢谢!

测试链表

class LinkedListTest 
{
    public static void main(String[] args) 
    {
            LinkedList list = new LinkedList();

            list.insertFirst("cat");
            list.insertFirst("dog");
            list.insertFirst("fish");
            list.insertFirst("cow");
            list.insertFirst("horse");
            list.insertFirst("pig");
            list.insertFirst("chicken");

            list.add(3, "mouse");

            list.print();
    }   
}

链表类

public class LinkedList 
{
    private Link first;

    public LinkedList() 
    {
        first = null;
    }

    public void insertFirst(String word)
    {
           Link link = new Link(word);
           link.next = first;
           first = link;

           System.out.print(first.item + " ");
    }

    public String deleteFirst()
    {   
        Link temp = first;
        first = first.next;
        return temp.item;
    }

    public String get(int index)
    {
        Link current = first;
        while (index > 0) 
         {
             index--;
             current = current.next;
         }

         return current.item;           
    }

    public void add(int index , String someString)
    {

        Link current = first;

        while (index>0)
        {
            index--;
            current = current.next;
        }

    Link newLink = new Link(someString);
    newLink.next = current;
    current = newLink;

    }

    public void print()
    {
        System.out.println("-----------PRINTING LIST------------");
        Link current = first;
        while(!(current==null))
        {
            System.out.println(current.item);
            current = current.next;
        }
    }
}

链接类

public class Link 
{   
    public String item;
    public Link next;

    public Link(String theItem) 
    {
        item = theItem;
    }
}

【问题讨论】:

  • 您是否遇到错误或唯一的问题是逻辑问题?
  • 没有错误,所以一定是逻辑。在测试链表类中,您可以看到我尝试在位置 3 添加鼠标....但是当我在添加鼠标后打印列表时,列表不包含鼠标...它只是打印列表的最后一个版本好像我从来没有添加任何新东西一样。

标签: java linked-list


【解决方案1】:

当插入这样的列表时。您将不得不设置两个“下一个”链接。下一个指向您正在插入的项目,下一个指向您正在插入的项目,指向您正在滑过的项目。

希望这会有所帮助。

【讨论】:

    【解决方案2】:
    newLink.next = current;
    current = newLink;
    

    LinkedList 类的add 方法中的上述代码应为:-

    newLink.next = current.next;
    current.next = newLink
    current = newLink;
    

    我希望这可以解决您的问题。您需要设置两个下一个链接以在中间插入一个节点。一个在当前节点指向下一个节点,一个在前一个节点指向当前节点。

    【讨论】:

    • oooooh...我知道现在发生了什么。非常感谢您的帮助。
    • @ThisBetterWork。啊!!对不起。最后一行应该是相反的。 current = newLink。效果如何?? ;)
    • @ThisBetterWork。但我认为这无关紧要,因为您必须将电流设置回第一个。
    • 它对我来说无论哪种方式都有效,但我想我会在干擦板上画出所有内容,以确保一切正常。再次感谢,非常有帮助:D
    【解决方案3】:

    查看您将 newLink 添加到当前列表的位置。提示......你不是。您更新了 current,这是一个指向 newLink 的局部变量,但您从未将 newLink 设置为当前链表中实际存在的任何内容的 next

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2018-01-29
      • 1970-01-01
      • 2019-01-17
      相关资源
      最近更新 更多