2012-04-18 82 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 2012-04-18 17:03:09

+0

@Colleen现在翻译代码。感谢您的帮助! – 2012-04-18 17:09:07

+0

@Colleen现已翻译。 – 2012-04-18 17:59:13

回答

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 2012-04-18 17:28:31