Java 循环双链表为什么我被困在循环中? 公共类链接列表{ 专用节点头; 私有节点尾部; /*插入*/ 公共void insertatead(E数据) { Node newNode=新节点(数据); if(this.head==null) { this.head=newNode; //newNode.nextNode=this.head;public void insertthead(E数据) { Node newNode=新节点(数据); if(this.head==null) { this.head=newNode; newNode.nextNode=this.head;//这不应该是错误的地方 newNode.prevNode=this.head;//添加此 this.tail=this.head; } 否则{ newNode.prevNode=this.tail; this.head.prevNode=newNode; newNode.nextNode=this.head; this.head=newNode; tail.nextNode=this.head//添加此 System.out.println(“tail.next是:“+this.tail.nextNode”); } } 公共无效插入AD(E数据) { Node newNode=新节点(数据); if(this.head==null) { this.head=newNode; newNode.nextNode=this.head;//这不应该是错误的地方 newNode.prevNode=this.head;//添加此 this.tail=this.head; } 否则{ newNode.prevNode=this.tail; this.head.prevNode=newNode; newNode.nextNode=this.head; this.head=newNode; tail.nextNode=this.head//添加此 System.out.println(“tail.next是:“+this.tail.nextNode”); } }
错误在于我的长度和toString方法的小修改,谢谢帮助。错误在于我的长度和toString方法的小修改,谢谢帮助。this.tail.nextNode=newNode;我想else语句中缺少了它。我也尝试过了,但tail.nextNode仍然给我一个无限循环。this.tail.nextNode=newNode;我想else语句中缺少了。我也试过了,但是tail.nextNode仍然给我一个无限循环。它仍然进入一个无限循环。:(@Chabba把整个代码都放出来了。。让我们调试一下!啊,我想它在我的toString方法中,让我尝试修复它仍然进入无限循环。:(@Chabba把整个代码都放出来了..让我们把它调试出来吧!啊,我想它在我的toString方法中,让我尝试修复它Java 循环双链表为什么我被困在循环中? 公共类链接列表{ 专用节点头; 私有节点尾部; /*插入*/ 公共void insertatead(E数据) { Node newNode=新节点(数据); if(this.head==null) { this.head=newNode; //newNode.nextNode=this.head;public void insertthead(E数据) { Node newNode=新节点(数据); if(this.head==null) { this.head=newNode; newNode.nextNode=this.head;//这不应该是错误的地方 newNode.prevNode=this.head;//添加此 this.tail=this.head; } 否则{ newNode.prevNode=this.tail; this.head.prevNode=newNode; newNode.nextNode=this.head; this.head=newNode; tail.nextNode=this.head//添加此 System.out.println(“tail.next是:“+this.tail.nextNode”); } } 公共无效插入AD(E数据) { Node newNode=新节点(数据); if(this.head==null) { this.head=newNode; newNode.nextNode=this.head;//这不应该是错误的地方 newNode.prevNode=this.head;//添加此 this.tail=this.head; } 否则{ newNode.prevNode=this.tail; this.head.prevNode=newNode; newNode.nextNode=this.head; this.head=newNode; tail.nextNode=this.head//添加此 System.out.println(“tail.next是:“+this.tail.nextNode”); } },java,data-structures,doubly-linked-list,circular-list,Java,Data Structures,Doubly Linked List,Circular List,错误在于我的长度和toString方法的小修改,谢谢帮助。错误在于我的长度和toString方法的小修改,谢谢帮助。this.tail.nextNode=newNode;我想else语句中缺少了它。我也尝试过了,但tail.nextNode仍然给我一个无限循环。this.tail.nextNode=newNode;我想else语句中缺少了。我也试过了,但是tail.nextNode仍然给我一个无限循环。它仍然进入一个无限循环。:(@Chabba把整个代码都放出来了。。让我们调试一下!啊,我想它在
public class LinkedList<E> {
private Node<E> head;
private Node<E> tail;
/* Inserts*/
public void insertAtHead(E data)
{
Node<E> newNode=new Node<E>(data);
if(this.head==null)
{
this.head=newNode;
//newNode.nextNode=this.head; <--- Here is error cause
this.tail=this.head;
}
else {
newNode.prevNode = this.tail;
this.head.prevNode = newNode;
newNode.nextNode = this.head;
this.head = newNode;
System.out.println("tail.next is: " + this.tail.nextNode);
}
}
public void insertAtHead(E data)
{
Node<E> newNode=new Node<E>(data);
if(this.head==null)
{
this.head=newNode;
newNode.nextNode=this.head; // this should NOT be the bug place
newNode.prevNode=this.head; // add this
this.tail=this.head;
}
else {
newNode.prevNode = this.tail;
this.head.prevNode = newNode;
newNode.nextNode = this.head;
this.head = newNode;
tail.nextNode = this.head // add this
System.out.println("tail.next is: " + this.tail.nextNode);
}
}