2014-11-19 2 views
0

다음 작업에서는 이름과 성을 해시 테이블에 넣어야합니다. 이름은 키이고 성은 값입니다. 이름과 성을 다시 입력해야합니다. 이름과 성을 입력 할 때마다 해시 테이블에 같은 값이 있는지 확인해야합니다. 그리고 동등한 값이 있으면 '같은 이름과 성이 있습니다'와 같은 것을 인쇄해야합니다. 우리 교수가 Java에서 제공 한 전통적인 해시 가져 오기가 아니라 해시를 위해 데이터 구조를 사용해야합니다. 내 문제는 내 해시 테이블을 검색하는 방법을 모르겠다. CBTH 클래스에 주어진 검색 방법을 사용하여 코드에 넣을 것이다.HashTable 검색

public class HashLozinki { 
public static void main (String[] args) throws IOException { 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    int N = Integer.parseInt(br.readLine()); 

      CBHT<Korisnici,String> table1 = new CBHT<Korisnici,String>(26); 
    for(int i=1;i<=N;i++){ 
     String imelozinka = br.readLine(); 
     String[] pom = imelozinka.split(" "); 
        table1.insert(new Korisnici(pom[0]), new String(pom[1])); 
    } 

    System.out.println(table1); 

    for(int i=1; i<=N; i++){ 
     String korisnik = br.readLine(); 
     String[] res = korisnik.split(" "); 
     table1.search(res[0]); // Here is my problem :S don't know how to use search 
    } 

} 
} 

// The Search Method (part of CBTH class).. i don't know how to implement it 
public SLLNode<MapEntry<K,E>> search(K targetKey) { 
    // Find which if any node of this CBHT contains an entry whose key is 
    // equal 
    // to targetKey. Return a link to that node (or null if there is none). 
    int b = hash(targetKey); 
    for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) { 
     if (targetKey.equals(((MapEntry<K, E>) curr.element).key)) 
      return curr; 
    } 
    return null; 
} 

답변

1

클래스 SLLNode는 값을 반환하는 방식 (또는 MapEntry)를 가져야한다.

SLLNodehere의 구현을 발견했습니다. 불행히도, 클래스 SLLNode에는 공개 메소드/필드가 없으므로 클래스를 동일한 패키지 (또는 동일한 파일)에 추가해야합니다. 체인 전화를 통해 가치를 얻을 수 있습니다 :

table1.search(res[0]).element.value 
+0

대단히 고마워요! 너 놀랍다 :) (Y) – user3075088