2017-11-29 6 views
-1

이 방법으로 문제가 있습니다. 주어진 인덱스에 값을 삽입한다고 가정합니다.재귀 - 목록의 앞과 끝에 값을 입력하십시오.

public Element insertElementAt(int value, int index) { 

    if (index == 0 || this.next == null) { 
     Element newElement = new Element(); 
     System.out.println("you are in the loop"); 
     newElement.setNext(this); 
     System.out.println("you are still in the loop"); 
     return this; 
    } 
else if (this.next !=null) { 
     index--; 
     this.next = this.next.insertElementAt(value, index); 
     return this; 
    } 
    if (this.next== null){ 
     return this; 
    } 
    return this; 
} 

아이디어는 방법은 중간에 내 요소의 끝에서, 전면에 값을 삽입 할 수 있어야한다. 나는 여전히 꽤 재귀 적이기는하지만이 부분까지 올 수 있었다. 왜 System.out.println을 추가했는지.
나는 라인/2를 변경/추가/삭제하기 만하면되지만, 어디서 구별 할 수는 없다고 확신한다. 또한 if-statement에 있다고 확신합니다. 다만 경우에

테스트 케이스 :

@Test 
public void testInsertElementAt_Front() { 
    Element el = createElements(0, 1, 2); 
    Element result = el.insertElementAt(11, 0); 
    System.out.println(result.showValues()); 
    assertEquals(11, result.getValue()); 
    assertEquals(0, result.getNext().getValue()); 
} 

@Test 
public void testInsertElementAt_Middle() { 
    Element el = createElements(0, 1, 2); 
    Element result = el.insertElementAt(11, 1); 
    System.out.println(result.showValues()); 
    assertEquals(0, result.getValue()); 
    assertEquals(11, result.getNext().getValue()); 
} 

@Test 
public void testInsertElementAt_End() { 
    Element el = createElements(0, 1); 
    System.out.println(el.showValues()); 
    Element result = el.insertElementAt(11, 2); 
    System.out.println(result.showValues()); 
    assertEquals(0, result.getValue()); 
    assertEquals(1, result.getNext().getValue()); 
    assertEquals(11, result.getNext().getNext().getValue()); 
    assertNull(result.getNext().getNext().getNext()); 
} 

그리고 출력 :

testInsertElementAt_Front() 경우 :
junit.framework.AssertionFailedError: expected:<11> but was:<0>

testInsertElementAt_Middle() 경우 :
junit.framework.AssertionFailedError: expected:<11> but was:<1>

testInsertElementAt_End의 경우 : java.lang.NullPointerException이 어떤 도움이

을 감상 할 수있다

코드의 포맷합니다 (다른 경우) 코드를 복사 할 때, 그것은 무슨 일이 있었의 비트가, 그것은

이 인 문제가되지 않습니다 요소 클래스 :

public class Element { 

private int value; 
private Element next; 

/** 
* 
* @return 
*/ 
public int getValue() { 
    return value; 
} 

/** 
* 
* @param value 
*/ 
public void setValue(int value) { 
    this.value = value; 
} 

/** 
* 
* @param next 
*/ 
public void setNext(Element next) { 
    this.next = next; 
} 

public Element getNext() { 
    return next; 
} 

/** 
* 
* @param value 
* @return 
*/ 
public Element appendElement(int value) { 
    if (this.next == null) { 
     Element newElement = new Element(); 
     newElement.setValue(value); 
     this.next = newElement; 
    } else { 
     this.next = this.next.appendElement(value); 
    } 
    return this; 
} 

/** 
* 
* @param value 
* @return 
*/ 
public Element insertElementSorted(int value) { 
    if (this.value > value) { 
     Element newElement = new Element(); 
     newElement.setValue(value); 
     newElement.setNext(this); 
     return newElement; 
    } else if (this.next == null) { 
     Element newElement = new Element(); 
     newElement.setValue(value); 
     this.next = newElement; 
     return this; 
    } else { 
     this.next = this.next.insertElementSorted(value); 
     return this; 
    } 
} 

/** 
* 
* @param value 
* @return 
*/ 
public Element deleteElement(int value) { 
    if (this.value == value) { 
     return this.next; 
    } else { 
     if (this.next != null) { 
      this.next = this.next.deleteElement(value); 
     } 
     return this; 
    } 
} 

/** 
* 
* @return 
*/ 
public int size() { 
    if (this.next == null) { 
     return 1; 
    } else { 
     return 1 + this.next.size(); 
    } 
} 

/** 
* 
* @return 
*/ 
public int sum() { 
    if (this.next == null) { 
     return this.value; 
    } else { 
     return value + this.next.sum(); 
    } 
} 

/** 
* 
* @return 
*/ 
public boolean isSorted() { 
    if (this.next == null) { 
     return true; 
    } else if (this.value > this.next.value) { 
     return false; 
    } else { 
     return this.next.isSorted(); 
    } 
} 

/** 
* 
* @param value 
* @return 
*/ 
public boolean existsElement(int value) { 
    if (this.value == value) { 
     return true; 
    } else if (this.next == null) { 
     return false; 
    } else { 
     return this.next.existsElement(value); 
    } 
} 

/** 
* 
* @return 
*/ 
public String showValues() { 
    if (this.next == null) { 
     return this.value + ""; 
    } else { 
     return this.value + " " + this.next.showValues(); 
    } 
} 

/** 
* 
* @param index 
* @return 
*/ 
public int getValueAt(int index) { 
    if (index == 0) { 
     return this.value; 
    } else if (this.next == null || index < 0) { 
     return Integer.MAX_VALUE; 
    } else { 
     index--; 
     return this.next.getValueAt(index); 
    } 

} 

/** 
* 
* @param value 
* @param index 
* @return this 
*/ 
public Element insertElementAt(int value, int index) { 
    if (index == 0 || this.next == null) { 
     Element newElement = new Element(); 
     newElement.setNext(this); 
     return newElement; 
    } 
    else if (this.next != null) { 
     index--; 
     this.next = this.next.insertElementAt(value, index); 
     return this; 
    } 
    if (this.next == null) { 
     return this; 
    } 
    return this; 
} 

/** 
* 
* @param value 
* @return element 
*/ 
public Element insertElementAtFront(int value) { 
    Element element = new Element(); 
    element.setValue(value); 
    element.setNext(this); 

    return element; 
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
} 

}

답변

0

당신은 아마 새로 만든 요소를 ​​반환해야 내가 n 귀하의 조건부 성명서의 첫 부분 :

if (index == 0 || this.next == null) { 
    Element newElement = new Element(); 
    newElement.setNext(this); 
    return newElement; 
} 
+0

그렇지 않습니다. 아무런 차이가 없다. – KongRasmus

+0

'Element'의 구현을 제공해 주시겠습니까? 나는 다른 모습을 가질 수있다. –

+0

방금 ​​내 게시물에 추가했습니다. – KongRasmus