2012-12-06 5 views
0

저는 자바 초보자이며 합병증 때문에 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; 
    } 
} 

입니다. 노드가 검색되면 발견 된 노드는 검색된 노드 다음의 노드를 가리 킵니다. 내가 노드를 검색 할 때마다

enter image description here

나는 내 코드 끔찍한에 기본적으로 변환 java.lang.nullpointer 예외를 얻을.

내가 어떻게해야 하는지를 검색 할 때마다 나는 항상 "왜 이것을 넣어야합니까?"또는 "LS는 무엇입니까?"또는 "왜 여러 가지 방법이 있으며 그 안에 변수 n은 무엇입니까?"

무엇이 잘못하고 무엇을해야하는지 가르쳐주세요.

+0

이 해결책이 아닐 수도 있지만 (예외가 발생하는 행을 알 수없는 경우 솔루션을 쉽게 제공 할 수 없음) 검색의 while 루프는 한 번만 반복됩니다. '} while (found = false);'는'found = false;와 같습니다. } while (found);'. 대신 동등성을 검사해야합니다 :'} while (found == false);'. – Vulcan

+0

그게 쉬운 부분 같아. 그러나 노드를 할당 할 때 더 까다로워 보일 때 –

답변

0
node = new Node (inputdata); 
      node.next = head; 

      // update the head to point to the new front of the list 
      head = node; 
      count++; 

당신은 당신이 노드에서 다음 하나가 머리 ... 라고 한 다음 그래서 당신은 기본적으로 만들고있어 ... 가서 머리는이 노드 텟 말, 노드를 만들 1 머리 == 노드 ==는 node.next

단지하지 않는다 : 나는 제안 D

을이 :

//Init head and tail... 
if(head==null){ 
head = new Node("Head"); //use whatever data you want/need 
tail= new Node("Tail"); 
tail.next=head; 
head.next = tail; 
} 

//add a new node... 
newnode = new Node("Some data"); 
//since this is a one-way linked list, i suggest you walk from the head 
//and go until you meet the tail 
currNode = head; 
while(currNode.next.data.compareTo("Tail") != 0) 
{ 
    currNode = currNode.next; 
} 
//now add the new node here... 
newnode.next = currNode.next; 
currNode.next = newNode; 

항상의 "끝"에 추가이 방법 목록 ... 당신은 머리 뒤에 사용할 수 있도록, 시작 부분에 추가하려는 경우 :

newNode = new Node("Some data"); 
    newNode.next = head.next; 
    head.next = newNode; 

그것은 당신이 목록의 마지막에있을 때 당신이 알고있는 꼬리와 같은 "제한"을 가지고 adviseable입니다 ...

그래서 지금 당신의 삭제 작업을해야하지만, 나는 몇 가지 추천 : 당신은 끝/꼬리까지 목록을 통과하고 '년후 경우 중단됩니다이 while 루프로

currentNode = head; 
do 
    { 
     if(currentNode.next.data.compareTo(delete)==0){ //if the next one is the one i'm looking for, remove it and let the garbage collector take care of it 
      currentNode.next = currentNode.next.next; 
      break; //leave the loop 
     else 
      currentNode = currentNode.next; 
    } 
    while (currentNode.next.data.compareTo("Tail") != 0); 

을 찾을 수 없습니다 ... 예를 들어, 영원히 목록 주위를 돌아 다니고, 검색된 노드를 찾을 수 없으므로

+1

아우 슈팅, 게시일을 잊어 버렸습니다 :/ 오, 글쎄, 미래에 누군가를 돕기를 바랍니다. – DaMachk

관련 문제