我创建了一个toString递归函数,当我在函数中首先更改为last以使其向后打印时,它不会输出任何内容。看起来它只是一遍又一遍地调用函数。
public String recursiveBackwardsString() {
if (this.isEmptyList()) {
return ("");
} else {
DoubleLinkedList < T > otherList = new DoubleLinkedList < T > ();
otherList.copy(this);
otherList.deleteNode(otherList.last.info);
return ("" + this.last.info + otherList.recursiveBackwardsString());
}
}
和我的toString有效:
public String recursiveToString() {
if (this.isEmptyList()) {
return ("");
} else {
DoubleLinkedList < T > otherList = new DoubleLinkedList < T > ();
otherList.copy(this);
otherList.deleteNode(otherList.first.info);
return ("" + this.first.info + otherList.recursiveToString());
}
}
有人能告诉我backwardsToString的问题吗?
public void deleteNode(T deleteItem){
DoubleLinkedListNode<T> current= new DoubleLinkedListNode<T>();
current=first;
if(this.isEmptyList()){
System.out.println("List is empty");
return;
}
else if(first.info.equals(deleteItem)){
if(count>1){
first=first.next;
first.back=null;
count=count-1;
return;
}
else{
first=null;
last=first;
count=0;
return;
}}
else{
current=first.next;
while(current!= null){
if(current.info==deleteItem){
current.back.next=current.next;
current.next.back=current.back;
count=count-1;
return;
}
}}
System.out.print("Item not in the list");
return;
}
答案 0 :(得分:1)
你需要改变这个:
else{
current=first.next;
while(current!= null){
if(current.info==deleteItem){
current.back.next=current.next;
current.next.back=current.back;
count=count-1;
return;
}
}}
为:
else{
current=first.next;
while(current!= null){
if(current.info.equals(deleteItem)){
current.back.next=current.next;
current.next.back=current.back;
count=count-1;
return;
}
}}
请注意==
至.equals()
。