특정 값이 내 연결된 목록에서 삭제 될 때까지 실행해야하는 while 루프가 있습니다. (remainingPoints) 문제는 해당 노드가 연결된 목록의 유일한 노드 일 경우 내 컴파일러에서 오류가 발생한다는 것입니다. 편집 : 나는 자바에서 미리 링크 된 목록 클래스를 사용하고 있습니다.Java의 링크 된 목록에서 유일한 노드를 제거하려면 어떻게합니까?
while (remainingPoints.contains(endPoint)) {
//loop through each index of the start points adjacency array and update the path estimates.
for (int i = 0; i < maxIndex; i++) {
//if the path estimate is greater than the distance found from the next Point to the
//i'th point, update the pathEstimate for that point and update the parent of the point.
if ((pathEstimates[i] != 0 && adjMatrix[next][i] != 0) && (adjMatrix[next][i]+pathEstimates[next] < pathEstimates[i])) {
pathEstimates[i] = adjMatrix[next][i] + pathEstimates[next];
parents[i] = next;
}
}
//reset next.
next = -1;
//This will be the intersection that has the shortest path from the last tested
//intersection (that is not 0).
for (int i = 0; i < maxIndex; i++) {
if (pathEstimates[i] != 0 && remainingPoints.contains(i)) {
if (next == -1)
next = i;
else if (pathEstimates[i] < next)
next = i;
}
}
//Inelegent solution goes here in place of the line of code below:
remainingPoints.remove(next);
}
내가 노력 세련되지 솔루션을 포함 -1 다음이 포함 된 노드가 while 루프의 다음 반복이 거짓하게 삭제 될 수 있도록 쓸모없는 노드를 추가 한이 if 문에 추가 할 수 있었다, 그러나 이것은 추가 더 궁금한 문제 :
if (remainingPoints.size() == 1)
remainingPoints.add(-1);
System.out.println(next);
System.out.println(remainingPoints.remove(next));
이 시도한 루프를 사용하면 루프가 무한히 실행됩니다. 다음에 인쇄되는 값은 1이고 (이것은 다음의 올바른 값이고 의도 된 값입니다.) 여하튼 remainingPoints.remove (next)의 값은 -1입니다. 다른 값도 테스트했으며 remainingPoints.remove (next)의 값은 항상 if 문을 사용하여 추가 한 값입니다. 이것은 remove 메소드가 무한 루프를 설명하는 추가 된 값을 제거하지만 이것이 발생하는 이유는 무엇입니까?
누구든지 java의 링크 된 목록에서 유일한 노드를 삭제하는 방법을 설명 할 수 있다면 대단히 감사하겠습니다 !! 위의 오류를 설명 할 수있는 사람에게는 보너스입니다.
제쳐두고, 스택 오버플로에 대한 첫 번째 게시물입니다. 게시 오류가 있거나 스택 오버플로 에티켓에 대해 잘 모르는 경우 알려 주시기 바랍니다.
head-> next가 null인지 아닌지 확인한 다음 머리의 값이 검색중인 값인지 확인한 다음 삭제하고 null을 반환하거나 아무것도하지 않으십시오. –
방금 java로 코딩을 시작한 프로그래머입니다. 어쩌면 이것은 바보 같은 질문이지만 노드를 삭제하고 null을 반환할까요? C로 코딩하고 있지만 자바로 미리 작성된 링크 목록 클래스를 사용하고 있으며 링크 된 목록에서 유일한 노드를 제거하는 방법이 없다고 생각하면 유용 할 것입니다. – user3792733
오, 당신이 질문에 언급하지 않았다면, 그냥 머리의 내용을 모두 null로 설정하십시오. 뭔가 같은 list.remove (0) –