2016-06-27 2 views
0

할당의 일부로 제네릭을 구현하면서 더미 헤드 노드와 함께 단일 링크 된 비 원형 LinkedList를 작성하고 있습니다. 이 할당에는 List 인터페이스 구현이 필요하지만 subList 메소드가 필요합니다. 나는 StackOverflow뿐만 아니라 일반적으로 내 자신의 디자인에 대한 여러 가지 다른 방법을 시도 했으므로 어떻게 완료되는지 예제를 보려고 노력하면서 웹을 수색했지만 subList의 변경 사항은 원래의 LinkedList에 반영되지 않습니다. 나는 (내가 어떤 헬퍼 메소드를하지 않을 정도로) 시도하고 here 최고 대답의 구조에 따라 내 방법을 재 작성하고 여기에 내 결과 코드 : 여기 LinkedList에 대한 subList 메서드 작성

@Override 
public List<E> subList(final int fromIndex, final int toIndex){//FIX ME 
    if(fromIndex < 0 || fromIndex > this.size()-1 || toIndex < 0 || toIndex > this.size()-1){ 
    throw new IndexOutOfBoundsException("Index out of bounds on call to subList with fromIndex of" 
    + fromIndex + " and toIndex of" + toIndex); 
    } 
    List<E> list = new LinkedList<E>(); 
    Node<E> cur = this.head.next; 
    int count = 0; 
    while(cur!=null){ 
    if(count >= fromIndex && count < toIndex){ 
     list.add(cur.data); 
    } 
    cur = cur.next; 
    count++; 
    } 

    return list; 
}// end sublist 

내 테스터의 조각입니다 파일, 당신은 그러나 원래 LinkedList의 반영하지 않는 하위 목록에서 변경, 내가 올바른 노드와 하위 목록을 만들 볼 수 있으며, 그 해결을 진행하는 방법을 잘 모르겠습니다 같이

New LinkedList has been created 
List before testing: [one, two, three, four, five] 
Testing subList function with fromIndex of 1, and toIndex of 4 
Printing subList: [two, three, four] 
Changing data of sublist to 'six, seven, eight' 
Printing subList: [six, seven, eight] 
Printing LinkedList after test: [one, two, three, four, five] 

I을 내 하위 목록으로 LinkedList를 사용하여 확실하지, 올바른 선택이며, 조언이나 비판 크게 받아 들여질 것입니다!

편집 : 나는 기본적으로 새로운 노드를 만들기보다는 원래 LinkedList의에 노드에서 직접 가리키고, 아래에있는 내 자신의 질문에 대답

답변

0
난 내 자신의 질문에 대답합니다

, 희망이 누군가에 유용 미래에 이것을 보려고합니다.

그래서 내 코드에서는 LinkedList에서 add (int index, E data) 함수를 사용했습니다.이 함수는 단순히 데이터 변수 자체를 변경하는 대신 새 노드를 작성하여 목록에 삽입했습니다. . 더 이상 새로운 노드를 생성되지 않도록 그래서 나는 그것을 재 작성하고 나는이 내 위의 하위 목록 클래스를 변경 :

이제
public List<E> subList(final int fromIndex, final int toIndex){//FIX ME 
    if(fromIndex < 0 || fromIndex > this.size()-1 || toIndex < 0 || toIndex > this.size()-1){ 
    throw new IndexOutOfBoundsException("Index out of bounds on call to subList with fromIndex of" + fromIndex + " and toIndex of" + toIndex); 
    } 
    LinkedList<E> list = new LinkedList<E>(); 
    Node<E> cur = this.head.next; 
    Node<E> pointer = list.head; 
    int count = 0; 
    while(cur!=null){ 
    if(count >= fromIndex && count < toIndex){ 
     pointer.next = cur; 
     list.size++; 
     pointer = pointer.next; 
    } 
    cur = cur.next; 
    count++; 
    } 
    return list; 

오히려 내가 직접의 노드에 내 원래 LinkedList의 노드를 할당 추가 기능을 사용하는 것보다 내 subList 및 크기를 수동으로 증가시킵니다.

불행히도 각 노드에 다음 참조가 있었기 때문에 수동으로 올바르게 추가 했더라도 toString 함수가 호출되면 다음 노드까지 'toIndex'를 지나서 노드를 포함하여 LinkedList를 반복적으로 반복합니다. null이었다. 이 문제를 해결하기 위해, 나는 크기를 포함 내 된 toString 추가 조건을 추가하고, 또한 반환 된 문자열에 쉼표를 추가 내 if 문을 변경 :

public String toString(){ 
    String ret = "["; 
    Node cur= this.head.next; 
    int index = 0; 
    while(cur != null && index < size){// added the index < size condition 
    ret = ret + cur.data; 
    if(index < this.size -1){// changed from cur.next != null 
     ret = ret + ", "; 
    } 
    cur = cur.next; 
    index++; 
    } 

    ret = ret + "]"; 
    return ret; 

}// end toString 

그래서 마지막으로, 내 테스트 출력은 다음과 같습니다 :

Testing subList function with fromIndex of 1, and toIndex of 4 
Printing subList: [two, three, four] 
Changing data of sublist to 'six, seven, eight' 
Printing subList: [six, seven, eight] 
Printing LinkedList after test: [one, six, seven, eight, five] 

것은 나는 아직도 배우고 내 코드는 적합하지 가능성이 있지만, 앞으로이 같은 문제로 다른 사람을 도울 수있는 희망이 응답!

관련 문제