저는 자바 초보자이며 합병증 때문에 Java에서 연결된 목록을 이해할 수 없습니다. 그래서 내 코드는 매우 간단합니다. 순환 링크 된 목록에서 노드를 제거하려면 어떻게합니까?
Node node, head, tail;
head = null; // initializes the head of the linked list to null
int counter = 0;
String inputdata; // input
do
{
System.out.print ("What name would you like stored? (\"quit\" to end) ");
inputdata = stdin.readLine();
if (!inputdata.equals ("quit"))
{
node = new Node (inputdata);
node.next = head;
// update the head to point to the new front of the list
head = node;
count++;
}
}
while (!inputdata.equals ("quit")); // loop continues until "quit" selected
System.out.println();
node = head;
///////////////////////////////
String delete;
boolean found;
System.out.println ("What node to delete?");
delete = stdin.readLine();
do
{
for (int i = 0 ; i <= count ; i++)
{
if (delete.equals (node.data))
{
found = true;
System.out.println ("It is found!");
}
}
}
while (found = false);
이
내가 알고리즘 작동 방식을 이해 클래스public class Node
{
Node next;
String data;
public Node (String data)
{
this.data = data;
}
}
입니다. 노드가 검색되면 발견 된 노드는 검색된 노드 다음의 노드를 가리 킵니다. 내가 노드를 검색 할 때마다
나는 내 코드 끔찍한에 기본적으로 변환 java.lang.nullpointer 예외를 얻을.
내가 어떻게해야 하는지를 검색 할 때마다 나는 항상 "왜 이것을 넣어야합니까?"또는 "LS는 무엇입니까?"또는 "왜 여러 가지 방법이 있으며 그 안에 변수 n은 무엇입니까?"
무엇이 잘못하고 무엇을해야하는지 가르쳐주세요.
이 해결책이 아닐 수도 있지만 (예외가 발생하는 행을 알 수없는 경우 솔루션을 쉽게 제공 할 수 없음) 검색의 while 루프는 한 번만 반복됩니다. '} while (found = false);'는'found = false;와 같습니다. } while (found);'. 대신 동등성을 검사해야합니다 :'} while (found == false);'. – Vulcan
그게 쉬운 부분 같아. 그러나 노드를 할당 할 때 더 까다로워 보일 때 –