2012-04-18 7 views
1

은 내가 대해 조금 혼란 스러워요이 학교의 과제가있다. 여기 링크 된 목록 배열

은 말하는 내용은 다음과 같습니다.

는 "해싱을 위해 '체인'의 기술을 사용하는 프로그램을 작성 이 프로그램은 링크 된 각 에 대한 참조를 포함하는 배열의 길이에 읽 프로그램이 링크리스트를 생성 한 경우에서 발생 될 것이다. 또한, 저장되는 모든 값이 판독된다. 이 프로그램은 인덱스가 존재하는 해싱을위한 별도의 기능을 가진다. 이론적 '부하율'인 계산하고 인쇄 할 수 있습니다. 전체 배열을 쉽게 인쇄해야합니다. "

내가 혼란스러워하는 부분은 생성 될 각 연결된 목록에 대한 참조를 포함하는 배열의 길이로 프로그램이 읽히는 부분입니다. 여러 연결된 목록을 생성 할 수 있습니까? 그렇다면 어떻게해야합니까?

public class EnkelLenke { 

    private Node head = null; 
    private int numOfElements = 0; 


    public int getNum() 
    { 
     return numOfElements; 
    } 

    public Node getHead() 
    { 
     return head; 
    } 

    public void insertInFront(double value) 
    { 
     head = new Node (value, head); 

     ++numOfElements; 
    } 

    public void insertInBack(double value) 
    { 
     if (head != null) 
     { 
      Node this = head; 

      while (this.next != null) 
       this = this.next; 
       this.next = new Node(value, null); 
     } 

     else 
      head = new Node(value, null); 
      ++numOfElements; 
    } 

    public Node remove(Node n) 
    { 
     Node last = null; 
     Node this = head; 

     while (this != null && this != n) 
     { 
      last = this; 
      this = this.next; 
     } 

     if (this != null) 
     { 
      if (last != null) 
       last.next = this.next; 
      else 
       head = this.next; 
       this.next = null; 
       --numOfElements; 
       return this; 
     } 

     else 
      return null; 
    } 

    public Node findNr(int nr) 
    { 
     Node this = head; 

     if (nr < numOfElements) 
     { 
      for (int i = 0; i < nr; i++) 
       this = this.next; 

      return this; 

     } 

     else 
      return null; 
    } 

    public void deleteAll() 
    { 
     head = null; 
     numOfElements = 0; 
    } 

    public String printAllElements() { 
     String streng = new String(); 

     Node this = head; 
     int i = 1; 

     while(this != null) 
     { 
      streng = streng + this.element + " "; 
      this = this.findNext(); 

      i++; 
      if(i > 5) 
      { 
       i = 1; 
       streng = streng + "\n"; 


      } 

     } 

     return streng; 
    } 

    public double getValueWithGivenNode (Node n) 
    { 

     Node this = head; 

     while (this != null && this != n) 
     { 
      this = this.next; 
     } 

     if (this == n) 
      return this.element; 
     else 
      return (Double) null; 

    } 
} 

public class Node { 

    double element; 
    Node next; 

    public Node(double e, Node n) 
    { 
     element = e; 
     next = n; 

    } 

    public double findElement() 
    { 
     return element; 
    } 

    public Node findNext() 
    { 
     return next; 
    } 

} 
+2

코드를 영문 번역본으로 제공하면 도움이됩니다. 이제 코드를 번역 – Colleen

+0

@Colleen. 도와 주셔서 감사합니다! –

+0

@Colleen 그것은 지금 번역. –

답변

4

귀하의 데이터 구조는 다음과 같이 보일 것입니다 ("LL"은 연결리스트입니다) : 각에서

i | a[i] 
------------------------------- 
0 | LL[obj1 -> obj5 -> obj3] 
1 | LL[obj2] 
2 | LL[] 
... | ... 
N-1 | LL[obj4 -> obj6] 

은 내가 사용하라고 해요 클래스입니다 배열 색인에는 해당 색인에 해시 된 객체의 링크 된 목록이 있습니다.

는 여러 링크 목록을 생성 할 수 있습니까? 그렇다면 어떻게해야합니까?

예. 배열을 만들고 각 요소를 새 연결 목록으로 초기화하십시오.

EnkelLenke[] a = new EnkelLenke[N]; 
for (int i = 0; i < N; i++) { 
    a[i] = new EnkelLenke(); 
} 
+0

좋습니다, 감사합니다! – Camilla