2017-12-06 1 views
-1

작은 문제 저는 여기 프로그램을 가지고 있습니다. 내 hashFunction에 의해 결정된 hashCode에 따라 배열 내의 연결된 목록에 단어를 추가하는 프로그램을 만들려고합니다. hashCode에 대한 값이 같으면 링크 된 목록에 추가됩니다. 나는 목록에 단어가 몇 번이나 있는지 계산하는 작은 계산법을 사용합니다. hashFunction의 값을 계산하여 작동합니다. 그런 다음 배열의 해당 값으로 이동하고 Null 값에 도달 할 때까지 LinkedList를 반복합니다. 그것은 목록에서 단어를 찾을 때마다 증가되는 count 변수를 가지고 있습니다.LinkedList에서 항목을 제거하십시오

public class test{ 

    public static class Node<T>{ 
     public T data; 
     public Node<T> next; 
     public Node(){ 

     } 

     public Node(T data, Node<T> next) 
     { 
      this.data = data; 
      this.next = next; 
     } 
    } 

    static Node[] array = new Node[512]; 

    public static void add(String word){ 
     int position = hashFunction(word); 
     if(array[position] == null){ 
      array[position] = new Node(word, null); 
     }else{ 
      Node newHead = new Node(word, array[position]); 
      array[position] = newHead; 
     } 
    } 

    public static void remove(String word){ 
     int remove = hashFunction(word); 
     Node head = array[remove]; 
     if(head.data == word){ 
      head = head.next; 
      System.out.println("Found"); 
     }else if(head.data != word){ 
      for(; array[remove] != null; array[remove] = array[remove].next){ 
       if(array[remove].data == word){ 
        array[remove] = array[remove].next; 
       } 
      } 
      System.out.println("Yusuf"); 
     } 
    } 

    public static int count(String word){ 
     int number = 0; 
     int position = hashFunction(word); 
     for(; array[position] != null; array[position] = array[position].next){ 
      if(array[position].data == word){ 
       number++; 
      } 
     } 
     System.out.println(number); 
     return number; 
    } 

    public static int hashFunction(String a){ 
     int sum = 1; 
      for(int i = 0; i<a.length(); i++){ 
       char b = a.charAt(i); 
       int value = (int) b; 
       sum *= value; 
     } 
     return sum % array.length; 
    } 

    public static void addthings(String word, int n){ 
     for(int i = 0; i<n; i++){ 
      add(word); 
     } 
    } 

    public static void main(String[] args) { 
     addthings("abc", 500000); 
     count("abc"); 
     count("abc"); 
     count("abc"); 
     count("abc"); 
     } 
} 

내 문제는 내가 값을 추가하고 잘 작동 발생하지만 그 이후의 계산 방법에 대한 더 많은 통화가 어떤 이유로 0을 반환하는 방법을 몇 번이나 확인이 처음이다 : 이것은 내 코드입니다.

내 제거 방법이 연결된 목록에서 항목을 제거하지 못하는 문제가 너무 있습니다. 이 코드는 List를 반복하고, 제거 할 항목을 찾으면 거기에서 포인터를 제거하고 다음 값을 가리 킵니다. 그러나 이것은 작동하지 않습니다.

누군가이 두 가지 문제를 해결하는 방법을 알려주실 수 있습니까?

감사합니다.

+0

첫 번째 호출에서 count() 메서드 내부의 루프가 실행되고 따라서 500000을 반환하지만 모든 후속 호출에서 루프를 입력하지 않으므로 array [position]! = null; 거짓으로 평가됩니다, 나는 어떻게되는지보고 계속 찾고, 저녁 식사 후에 다시 게시됩니다. – prsvr

+0

루프 in count()는 처음으로 초기화됩니다. array [position] = array [position] .next 배열에 null을 할당하고 루프/count()가 null 일 때 null이 아닙니다. 첫 번째 시간 배열 [38] (array [position])은 [email protected]이고 그 이후의 모든 호출에 대해 null이므로 배열 [position]! = null; false이고 루프가 실행되지 않으므로 number는 기본적으로 0으로 초기화됩니다. – prsvr

답변

0

무언가를 쓸 경우 Node head은 노드에 대한 일부 로컬 인스턴스를 작성 중임을 의미합니다. head = head.next을 설정하면 배열의 상태가 아닌 로컬 인스턴스 변수의 상태가 변경됩니다.

첫 번째 노드에 찾고있는 데이터가 포함되어 있는지 확인하고 제거하려고하면 원본 배열 (참조가있는 배열)에서 제거해야합니다. 다음과 같이 작성할 수 있습니다.

if(head.data == word) 
    array[remove] = head.next; 

이것은 예입니다. 요점은 배열에서 물건을 chaning하지 않고 로컬 변수에있는 것입니다.

public static void remove(String word){ 
      int remove = hashFunction(word); 
      Node head = array[remove]; 
      if(head.data == word){ 
       head = head.next; 
       System.out.println("Found"); 
      }else if(head.data != word){ 
       for(; array[remove] != null; array[remove] = array[remove].next){ 
        if(array[remove].data == word){ 
         array[remove] = array[remove].next; 
        } 
       } 
       System.out.println("Yusuf"); 
      } 
     } 

두 번째 실수는 당신이 단지 그것은 두 개의 서로 다른 LinkedList의로 LinkedList의 휴식 것입니다 array[remove] = array[remove].next; 을 설정 두 번째 절입니다. 연결된 목록 A,B,C,D에 4 개의 요소가 있고 B을 제거하고 A->B->C->D과 같은 포인터가 있다면 A -> C에서 포인터를 추가하고 있다고 가정합니다. 여기 링크드리스트를 깨뜨릴 수 있습니다.

쉽게 사용할 수있는 while 루프를 사용할 수 있습니다.

관련 문제