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; 
    }

}
4

1 回答 1

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();
 }
于 2012-04-18T17:04:39.503 回答