2012-04-18 18 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

関連する問題