繁体   English   中英

尝试以递归方式将自定义节点添加到LinkedList的末尾

[英]Trying to add a custom Node to the end of a LinkedList recursively

嗨,我似乎很难在自定义列表的后面添加自定义节点。 自定义节点称为ListNode ,而链接列表称为AddressList

我的程序不会崩溃或引发任何异常,但不会将ListNode添加到我的AddressList的末尾。 我的addToFront方法有效,但我的addToBack方法addToBack 我只需要有人看看我的addToBack方法,看看我哪里出了问题。

我还必须递归执行此操作。 每个ListNode具有一定的值( nametelephoneNumemailaddressdob ),也是一个Next值,它是一个ListNode应该指向下一ListNodeAddressList

这是我的代码:

public ListNode(String name, String telephoneNum, String email, String address, String dob) {
    this.name = name;
    this.telephoneNum = telephoneNum;
    this.email = email;
    this.address = address;
    this.dob = dob;
}

public ListNode getNext() {
   return next;
}

public void setNext(ListNode link) {
   next = link;
}

上面的代码部分是ListNode的构造函数,以及用于获取和设置AddressList的下一个链接的方法。

public void addToBack(String name, String telephoneNum, String email, String address, String dob) {
    /*Base case.*/
    /*If the next node in the AddressList is null add the ListNode to that node.*/
    if(currentNode.getNext() == null) {
        currentNode = currentNode.getNext();
        currentNode = new ListNode(name, telephoneNum, email, address, dob);
    }
    /*Recursive case.*/
    /*If the AddressList still has nodes after the currentNode, keep going.*/
    else {
        currentNode = currentNode.getNext();
        addToBack(name, telephoneNum, email, address, dob);
    }
    currentNode = head;
}

上面是我的addToBack方法。 我只是不明白为什么我的程序不会引发异常或将ListNode添加到AddressList的后面。 任何反馈将不胜感激。

这是令人讨厌的代码...

  /*Base case.*/
   /*If the next node in the AddressList is null add the ListNode to that node.*/
   if(currentNode.getNext() == null)
   {
      currentNode = currentNode.getNext();
      currentNode = new ListNode(name, telephoneNum, email, address, dob);
   }

如果达到空值情况,则需要将下一个节点设置为新节点。

   if(currentNode.getNext() == null)
   {
      currentNode.setNext(new ListNode(name, telephoneNum, email, address, dob));
   }

分开的责任

我认为您首先最好将一些职责分开:不要每次都使用参数来进一步推进该用户的信息,而是先构造一个节点,然后将其传递给递归方法。 这将提高性能,并使调用堆栈更小。 所以像这样:

public void addToBack(String name, String telephoneNum, String email, String address, String dob) {
    addToBack(new ListNode(name,telephoneNum,email,address,dob));
}

然后,您需要制定一种方法,例如:

public void addToBack(ListNode newNode) {
    //TODO: implement
    //...
}

避免对象中的方法状态(或延续状态)

第二个问题是您的AddressList似乎有一个currentNode字段,该字段在递归过程中被修改。 这可能会造成很大的问题:它将延续状态附加到您的AddressList 现在想象一下,稍后您想使您的类成为多线程,那么这些线程将同时读取和操作该字段。 简而言之:这是糟糕的设计,请使用方法变量和/或参数。

因此,我们要做的第一件事是获取AddressList的头并使用它:

public void addToBack(ListNode newNode) {
    this.addToBack(this.head,newNode);
}

这还不够:空的链表没有headheadnull引用。 在这种情况下,我们只需将head设置为newNode 因此我们将其重写为:

public void addToBack(ListNode newNode) {
    if(this.head == null) {
        this.head = newNode;
    } else {
        this.addToBack(this.head,newNode);
    }
}

现在显然我们仍然需要实现核心方法:

public void addToBack(ListNode current, ListNode newNode) {
    //TODO: implement
    //...
}

实施核心方法

当你确定你自己,基本上有两种情况:基本情况,其中.getNext()currentnull ,一个一个地方是不是:基础方案,我们简单地设置.setNextcurrent我们的newNode

if(current.getNext() == null) {
    current.setNext(newNode);
}

在后一种情况下,我们前进:获取.getNext节点,然后递归调用该方法:

else {
    addToBack(current.getNext(),newNode);
}

或一起:

public void addToBack(String name, String telephoneNum, String email, String address, String dob) {
    //separate responsibilities, by constructing the node first
    addToBack(new ListNode(name,telephoneNum,email,address,dob));
}

public void addToBack(ListNode newNode) {
    //do not use a continuation state in a class, fetch the head, inspect the head and if not null pass to the recursion method
    if(this.head == null) {
        this.head = newNode;
    } else {
        this.addToBack(this.head,newNode);
    }
}

public void addToBack(ListNode current, ListNode newNode) {
    //generic method that adds the node at the end
    if(current.getNext() == null) {//base case: current is the last node
        current.setNext(newNode);
    } else {//recursive case, current is not the next node
        addToBack(current.getNext(),newNode);
    }
}

通过防止两次调用.getNext方法,可以使最后一个方法快一些:

public void addToBack(ListNode current, ListNode newNode) {
    //generic method that adds the node at the end
    ListNode nx = current.getNext();
    if(nx == null) {//base case: current is the last node
        current.setNext(newNode);
    } else {//recursive case, current is not the next node
        addToBack(nx,newNode);
    }
}

但这只是一个细节,影响将非常有限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM