2017-10-27 13 views
0

私はtail要素で始まり、最初のもので終わる二重リンクリストを印刷しようとしています。以下の私の現在のコードはそれを行いますが、何らかの理由でdequedアイテムを返すこともあります。私が頭から尾までリストを印刷するとき、これはしません。このメソッドまたはdequedメソッドを引き起こすtoStringの場合はIdk。私は両方を含んだ。Java-逆のリストを二重に印刷する

public String toString() { 

    String result; 

    if (isEmpty()) 
     return "empty"; 
    else { 
     result = ""; 
     DoubleNode current = tail; 

     while (current != null) { 
      result = result + current.getElement() + " "; 
      current = current.getPrev(); 
     } 
    } 
    return result; 
} 


public Item dequeueBack() throws NoSuchElementException { 
    if (isEmpty()) 
     throw new NoSuchElementException("deque is empty"); 

    Item result = tail.getElement(); 
    tail = tail.getPrev(); 
    count--; 

    if (isEmpty()) 
     head = null; 
    else 
     tail.setNext(null); 

    return result; 
} 

答えて

1

デキュー時には何も設定しないので、印刷するリンクはそのまま動作します。