Loops 在双链接列表中插入无限循环的方法
我可能完全不知道这里发生了什么,但是我已经看过它一百万次了,我确信这是很明显的,但是我的insert方法由于while循环而无限循环。任何帮助都将不胜感激 这是密码Loops 在双链接列表中插入无限循环的方法,loops,while-loop,linked-list,infinite,doubly-linked-list,Loops,While Loop,Linked List,Infinite,Doubly Linked List,我可能完全不知道这里发生了什么,但是我已经看过它一百万次了,我确信这是很明显的,但是我的insert方法由于while循环而无限循环。任何帮助都将不胜感激 这是密码 package dcjaniszewskiOL4; public class DoublyLinkedList<T extends Comparable> { private class Node<T> { private T data; pri
package dcjaniszewskiOL4;
public class DoublyLinkedList<T extends Comparable> {
private class Node<T>
{
private T data;
private Node<T> next;
private Node<T> prev;
private Node(T d){
data=d;
next=null;
prev=null;
}
private Node( T d, Node<T> pref, Node<T> nref){
data=d;
prev=pref;
next=nref;
}
}
private Node<T> head;
private Node<T> current;
private int size;
public DoublyLinkedList(){
head = new Node<T>(null,null,null);
current=head.next;
size=0;
}
public DoublyLinkedList(DoublyLinkedList<T> i){
}
public void insert (T d){
Node<T> ptr, trav, prev;
prev=null;
begin();
trav=head.next;
if(empty()){
head.next = new Node<T>(d,head,head);
}
while(trav!=null){
System.out.println(trav.data);
prev=trav;
advance();
trav=current;
}
ptr = new Node<T> (d, prev,head);
if(prev==null){
head.next=ptr;
head.prev=ptr;
}
else{
prev.next=ptr;
current.prev=ptr;
}
size++;
}
public void remove(T d) throws ListEmptyException, NotInListException{
Node<T> tmp = head;
if(head.next==null){
throw new ListEmptyException("List is empty on Remove");
}
while(tmp!=null){
if(tmp.data.equals(d)){
tmp.prev.next=tmp.next;
tmp.next.prev=tmp.prev;
size--;
}else{
tmp=tmp.next;
if(tmp==null){
throw new NotInListException("Item is not in the list");
}
}
}
}
public void begin(){
current=head.next;
}
public void advance(){
current.prev = current;
current=current.next;
}
public void retreat(){
current.next=current;
current=current.prev;
}
public T current() throws ListEmptyException{
if(current==null){
throw new ListEmptyException("List is empty");
}else{
return current.data;
}
}
public boolean end(){
boolean end = false;
if(current==null){
end=true;
}else{
end=false;
}
return end;
}
public boolean empty(){
boolean empty = false;
if(size()==0){
empty=true;
}else{
empty=false;
}
return empty;
}
public int size(){
return size;
}
}
包dcjaniszewskiOL4;
公共类双链接列表{
私有类节点
{
私有T数据;
私有节点下一步;
私有节点prev;
专用节点(TD){
数据=d;
next=null;
prev=null;
}
专用节点(td、节点pref、节点nref){
数据=d;
prev=pref;
next=nref;
}
}
专用节点头;
专用节点电流;
私有整数大小;
公共双链接列表(){
head=新节点(null,null,null);
当前=head.next;
尺寸=0;
}
公共双链接列表(双链接列表i){
}
公共空白插入(T d){
节点ptr、trav、prev;
prev=null;
begin();
trav=head.next;
if(空()){
head.next=新节点(d,head,head);
}
while(trav!=null){
系统输出打印LN(传输数据);
prev=trav;
前进();
trav=电流;
}
ptr=新节点(d,上一个,头部);
if(prev==null){
head.next=ptr;
head.prev=ptr;
}
否则{
上一个=下一个=ptr;
当前。上一个=ptr;
}
大小++;
}
public void remove(td)抛出ListMPtyException,NotInListException{
节点tmp=头部;
if(head.next==null){
抛出新的ListMPtyException(“删除时列表为空”);
}
while(tmp!=null){
if(tmp数据等于(d)){
tmp.prev.next=tmp.next;
tmp.next.prev=tmp.prev;
大小--;
}否则{
tmp=tmp.next;
if(tmp==null){
抛出新的NotInListException(“项目不在列表中”);
}
}
}
}
公共空间开始(){
当前=head.next;
}
预支{
current.prev=当前;
当前=当前。下一步;
}
公众休养会(){
当前。下一个=当前;
current=current.prev;
}
public T current()抛出ListMPtyException{
如果(当前==null){
抛出新ListentyException(“列表为空”);
}否则{
返回当前数据;
}
}
公共布尔结束(){
布尔结束=假;
如果(当前==null){
结束=真;
}否则{
结束=假;
}
返回端;
}
公共布尔空(){
布尔空=假;
如果(size()=0){
空=真;
}否则{
空=假;
}
返回空;
}
公共整数大小(){
返回大小;
}
}
试试这个
ptr = new Node<T> (d, null,null);
trav=head;
while (trav.next!= null)
{
trav = trav.next;
}
trav.next = ptr;
ptr.prev = trav;
ptr=新节点(d,null,null);
trav=头;
while(trav.next!=null)
{
trav=trav.next;
}
trav.next=ptr;
ptr.prev=trav;
如果它不起作用,请告诉我当您要求其他人阅读您的代码时,请使用空格。例如
if(size()==0){
。为什么要在advance()
中将current.prev
设置为current
?调用var pointer=current;而(pointer.prev==pointer){pointer=pointer.prev}
之后会无限循环。我知道现在已经很晚了,但我在发完这篇文章后很快就发现了问题所在,但我不想让你认为我让你挂断了,所以非常抱歉!谢谢你的帮助。