[英]Trying to add a custom Node to the end of a LinkedList recursively
嗨,我似乎很难在自定义列表的后面添加自定义节点。 自定义节点称为ListNode
,而链接列表称为AddressList
。
我的程序不会崩溃或引发任何异常,但不会将ListNode
添加到我的AddressList
的末尾。 我的addToFront
方法有效,但我的addToBack
方法addToBack
。 我只需要有人看看我的addToBack
方法,看看我哪里出了问题。
我还必须递归执行此操作。 每个ListNode
具有一定的值( name
, telephoneNum
, email
, address
, dob
),也是一个Next
值,它是一个ListNode
应该指向下一ListNode
在AddressList
。
这是我的代码:
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);
}
这还不够:空的链表没有head : head
是null
引用。 在这种情况下,我们只需将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()
的current
是null
,一个一个地方是不是:基础方案,我们简单地设置.setNext
的current
我们的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.