[英]compiler won't advance in recursive reversal of singly linked list
我在SinglyLinkedList
类中有一个递归静态方法,由于不确定的原因,该方法永远运行。 此类是通用的,其声明如下所示:
public class SinglyLinkedList<E>{
该类具有一个内部通用类Node
,它看起来像这样:
private static class Node<E> {
private E element;
private Node<E> next;
public Node(E e, Node<E> n) {
this.element = e;
this.next = n;
}
public E getElement() {
return this.element;
}
public Node<E> getNext() {
return this.next;
}
public void setNext(Node<E> n) {
this.next = n;
}
}
此外, SinglyLinkedList
类具有以下字段:
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
我遇到麻烦的方法称为reverse
,其目的是以递归的方式反向单链表的顺序。 这是此方法的代码:
public static SinglyLinkedList reverse(SinglyLinkedList l) {
if (l.size < 2) return l;
Node first = l.removeFirstNode();
SinglyLinkedList reversed = reverse(l);
reversed.addLast(first);
return reversed;
}
reverse
方法使用一种称为removeFirstNode
的非静态方法,其目的是删除单链列表中的第一个Node
并返回它:
private Node<E> removeFirstNode() {
if (isEmpty()) return null;
//Node<E> answer = new Node<>(this.head.getElement(), null);
Node<E> answer = this.head;
this.head = this.head.getNext();
this.size--;
if (this.size == 0) this.tail = null;
return answer;
}
reverse
方法还使用称为addLast
的非静态方法,其目的是将给定的Node
添加到单链表的末尾:
private void addLast(Node<E> n) {
if (isEmpty()) this.head = n;
else this.tail.setNext(n);
this.tail = n;
this.size++;
}
问题是,当我尝试在size
等于2的SinglyLinkedList
上运行reverse
方法时,编译器进入该行
reversed.addLast(first);
然后在addLast
方法中,它在行上停止
this.tail = n;
并且永远运行而不会终止。 如果size
等于或大于3,则编译器进入该行
reversed.addLast(first);
甚至不输入addLast
方法就停在那里。 现在如果我更换线
Node<E> answer = this.head;
与当前已注释掉的行
Node<E> answer = new Node<>(this.head.getElement(), null);
reverse
方法将终止,没有任何问题。 谁能解释这是怎么回事?
编辑:我刚刚意识到size
3或更大的不同行为仅仅是递归的产物。 真正的问题在于size
等于2且方法莫名其妙地在行终止的情况
this.tail = n;
编辑2:
最小代码:
public class SinglyLinkedList<E>{
private static class Node<E> {
private E element;
private Node<E> next;
public Node(E e, Node<E> n) {
this.element = e;
this.next = n;
}
public E getElement() {
return this.element;
}
public Node<E> getNext() {
return this.next;
}
public void setNext(Node<E> n) {
this.next = n;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
public SinglyLinkedList() {}
public int size() { return this.size; }
public boolean isEmpty() {
return this.size == 0;
}
public void addLast(E e) {
Node<E> newest = new Node<>(e, null);
if (isEmpty())
this.head = newest;
else
this.tail.setNext(newest);
this.tail = newest;
this.size++;
}
@Override
public String toString() {
String output = "";
if (this.size > 0) {
Node<E> current_node = head;
while (current_node != null) {
output += current_node.getElement();
if (current_node != tail) output += ", ";
else output += "\n";
current_node = current_node.getNext();
}
}
return output;
}
private void addLast(Node<E> n) {
if (isEmpty()) this.head = n;
else this.tail.setNext(n);
this.tail = n;
this.size++;
}
private Node<E> removeFirstNode() {
if (isEmpty()) return null;
//Node<E> answer = new Node<>(this.head.getElement(), null);
Node<E> answer = this.head;
this.head = this.head.getNext();
this.size--;
if (this.size == 0) this.tail = null;
return answer;
}
public static SinglyLinkedList reverse(SinglyLinkedList l) {
if (l.size < 2) return l;
Node first = l.removeFirstNode();
SinglyLinkedList reversed = reverse(l);
reversed.addLast(first);
return reversed;
}}
测试类别:
public static void main(String[] args) {
int n = 4;
SinglyLinkedList<Integer> list = new SinglyLinkedList<>();
for (int i = 1; i <= n; i++) list.addLast(i);
System.out.print(list);
System.out.print(SinglyLinkedList.reverse(list));
}
removeFirstNode()
实现不会取消链接该节点的下一个指针。
在原始链表中,第一个节点的下一个点指向第二个节点,第二个节点的下一个指针为null。
在新列表中,第一个节点的下一个指针将指向第二个节点,但是第二个节点的下一个指针将指向第一个节点(以前是第二个节点)。
这样的东西(原始列表):
+---+ +---+
| A |--->| B |--->null
+---+ +---+
由于A的下一个指针仍然指向B,因此重新排序成为了这种情况:
+---+ +---+
| B |--->| A |---+
+---+ +---+ |
^ |
| |
+--------------+
您可以将removeFirstNode()
实现更改为以下形式:
private Node<E> removeFirstNode() {
if (isEmpty()) return null;
Node<E> answer = this.head;
this.head = this.head.getNext();
answer.next = null; // Set next ptr to null
this.size--;
if (this.size == 0) {
this.tail = null;
}
return answer;
}
该代码可能看起来像“停止”,因为调试器尝试使用toString()
来打印出列表,我想它会遍历列表并且由于循环而无法完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.