2013-10-20 1 views
2

매개 변수에 지정된대로 인덱스 a에서 b까지 요소 목록을 역순으로 처리해야하는 doubley 연결 목록 데이터 구조에 대한 메서드를 작성하고이를 수행하기로했습니다. 재귀 적으로. 할당의 목적은 노드를 사용하여 포인터 조작을 연습하는 것이 었습니다. 내 메서드의 논리는 괜찮은 것 같았지만 JUnit 테스트를 통해 코드를 실행하면 끝나지 않을 것입니다. 이것이 이상하다고 생각하여 println 문을 추가하여 코드의 어느 부분에 도달했는지 확인합니다. 모든 것은 괜찮아. 그래서 Eclipse의 디버거를 통해 실행했고, 모든 재귀 호출을 앞뒤로 끝내고 종결하지 않고 끝 브래킷에 도달했습니다. 그냥 마지막 괄호에 앉아서, 나는 전에 이런 걸 본 적이 없어요. 왜 그렇게하고 있으며 그것을 고칠 수 있습니까?Java 재귀 메서드가 끝나지 만 종료되지 않습니다

public void reverseList(int start, int end) 
{ 
    if (start >= end) 
    { 
     return; 
    } 

    ListNode left = getListNode(start); 
    ListNode right = getListNode(end); 
    ListNode leftNext = left.next; 
    ListNode leftPrev = left.previous; 
    ListNode rightNext = right.next; 
    ListNode rightPrev = right.previous; 

    leftPrev.next = right; 
    rightPrev.next = left; 
    leftNext.previous = right; 
    rightNext.previous = left; 
    left.next = rightNext; 
    left.previous = rightPrev; 
    right.next = leftNext; 
    right.previous = leftPrev; 

    reverseList(start + 1, end - 1); 
} 

편집 : 여기

코드의 여기를 테스트하는 코드가

의 JUnit :

@Test 
public void testReveseList() 
{ 
StudentList list = new StudentList(); 
list.add("a", ""); 
list.add("b", ""); 
list.add("c", ""); 
list.add("d", ""); 
list.add("e", ""); 
list.add("f", ""); 
list.add("g", ""); 
list.add("h", ""); 
list.add("i", ""); 
list.add("j", ""); 
list.printlist(); 
list.reverseList(2, 5); 
System.out.println(); 
StudentList expectedList = new StudentList(); 
expectedList.add("a", ""); 
expectedList.add("b", ""); 
expectedList.add("f", ""); 
expectedList.add("e", ""); 
expectedList.add("d", ""); 
expectedList.add("c", ""); 
expectedList.add("g", ""); 
expectedList.add("h", ""); 
expectedList.add("i", ""); 
expectedList.add("j", ""); 
assertEquals(expectedList, list); 
list.reverseList(2, 5); 

System.out.println(); 

StudentList expectedList1 = new StudentList(); 
expectedList1.add("a", ""); 
expectedList1.add("b", ""); 
expectedList1.add("c", ""); 
expectedList1.add("d", ""); 
expectedList1.add("e", ""); 
expectedList1.add("f", ""); 
expectedList1.add("g", ""); 
expectedList1.add("h", ""); 
expectedList1.add("i", ""); 
expectedList1.add("j", ""); 
assertEquals(expectedList1, list); 
list.reverseList(0, 9); 
System.out.println(); 
StudentList expectedList2 = new StudentList(); 
expectedList2.add("j", ""); 
expectedList2.add("i", ""); 
expectedList2.add("h", ""); 
expectedList2.add("g", ""); 
expectedList2.add("f", ""); 
expectedList2.add("e", ""); 
expectedList2.add("d", ""); 
expectedList2.add("c", ""); 
expectedList2.add("b", ""); 
expectedList2.add("a", ""); 
assertEquals(expectedList2, list); 

} 

테스트 클래스 :

public class StudentList 
{ 
private ListNode head = null; 

public void add(StudentData data) 
{ 
    ListNode newNode = new ListNode(data); 
    ListNode lastNode = getTail(); 

    if (lastNode == null) 
    { 
     head = newNode; 
    } 
    else 
    { 
     lastNode.next = newNode; 
    } 

    newNode.previous = lastNode; 
    newNode.next = null; 

}  

    public ListNode getListNode(int indexOfDesiredNode) 
{ 
    if (indexOfDesiredNode >= size()) 
    { 
     // --- Error: There aren't that many nodes 
     return null; 
    } 

    // --- Move through the list, node by node, until we find 
    // --- the one we want 
    int count = 0; 
    ListNode current = head; 
    while ((count < indexOfDesiredNode) && (null != current)) 
    { 
     current = current.next; 
     count = count + 1; 
    } 

    // --- So, did we find anything? 
    if (null == current) 
    { 
     // --- Error: We didn't find the node 
     return null; 
    } 

    return current; 
} 

    public int size() 
{ 
    if (null == head) 
    { 
     return 0; 
    } 

    int count = 0; 
    ListNode current = head; 
    while (current != null) 
    { 
     count = count + 1; 
     current = current.next; 
    } 
    return count; 
} 

    public void reverseList(int start, int end) 
{ 
    if (start >= end) 
    { 
     return; 
    } 

    ListNode left = getListNode(start); 
    ListNode right = getListNode(end); 
    ListNode leftNext = left.next; 
    ListNode leftPrev = left.previous; 
    ListNode rightNext = right.next; 
    ListNode rightPrev = right.previous; 

    leftPrev.next = right; 
    rightPrev.next = left; 
    leftNext.previous = right; 
    rightNext.previous = left; 
    left.next = rightNext; 
    left.previous = rightPrev; 
    right.next = leftNext; 
    right.previous = leftPrev; 

    //-- The easy way of doing it... *sigh* 
    // StudentData temp = get(start); 
    // getListNode(start).data = getListNode(end).data; 
    // getListNode(end).data = temp; 

    reverseList(start + 1, end - 1); 
} 
} 

그리고 ListNode 클래스 :

public class ListNode 
{ 
public StudentData data = null; 
public ListNode next = null; 
public ListNode previous = null; 

public ListNode(StudentData data) 
{ 
    this.data = data; 
} 

@Override 
public String toString() 
{ 
    return data.toString(); 
} 
} 

서식이 가장 좋지 않은 점은 죄송합니다. & 붙여 넣기와 함께 발생했습니다.

+0

제대로 작동합니다. 문제는'getListNode' 호출에있을 수 있습니다. [SSCCE] (http://sscce.org/)를 게시 할 수 있습니까? – BackSlash

+0

꼬리 재귀 함수입니다. 따라서 마지막 순환 호출이 실제로 반환 된 후에는 함수의 끝 부분에 도달하여 각각의 연속 호출에서 내재적으로 복귀하게됩니다. 끝내지 않을거야? – chepner

+0

getListNode가 내 강사에 의해 작성되었으므로 나는 그것이 효과적이라는 것을 알고있다. 지정된 인덱스에 노드를 반환합니다. – TheBrogrammer

답변

0

end == start + 1이므로 rightPrev == left 인 경우 문제가 있습니다. 그런 다음 rightPrev.next = left을 설정하여 목록을 순환 시키므로 getListNode은 종료되지 않습니다.

관련 문제