2017-10-27 2 views
0

꼬리 요소로 시작하여 첫 번째로 끝나는 이중 연결된 목록을 인쇄하려고합니다. 아래의 현재 코드는 그 일을하지만 어떤 이유로 든 dequed 항목을 반환합니다. 머리에서 꼬리까지 목록을 인쇄 할 때이 작업은 수행되지 않습니다. idk가 this 또는 dequed 메소드를 발생시키는 toString인지 여부. 나는 둘 다 포함했다.자바 - 역순으로 이중 연결된 목록 인쇄

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

대기열에서 제외 할 때 아무것도 설정하지 않으므로 인쇄하는 링크는 여전히 작동합니다.