java - 链表数组

标签 java list hash linked-list

我有一份作业让我有点困惑。

内容如下:

"Write a program that uses the technique of 'chaining' for hashing. The program will read in the length of an array which will contain the reference to each linked list that will be generated. Furthermore, all values that are to be stored, is read. The program shall have a separate function for hashing where the index exists. When the program have generated the linked lists, the theoretical 'load factor' is to be calculated and printed out. The whole array should be easily printed out."

让我感到困惑的是,关于程序的部分将读取数组的长度,该数组将包含对将生成的每个链表的引用。是否可以生成多个链表?在那种情况下,你是怎么做到的?

这是我被告知要使用的类:

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

}

最佳答案

您的数据结构将如下所示(其中“LL”是一个链表):

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

在每个数组索引处,您都有一个散列到该索引的对象的链接列表。

Is it possible to generate multiple linked lists? In that case, how do you do that?

是的。创建数组,并将每个元素初始化为新的链表。

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

关于java - 链表数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10214239/

相关文章:

java - 将许多 vector 同步在一起

java - 而 else 语句等同于 Java?

javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated in jdk7 but not in jdk8

python - 在更短的时间内(优化时间)从另一个列表中删除一个列表中的所有值

c# - LINQ ForEach 可以有 if 语句吗?

python - 将字符串中的日期列表转换为 Python 中的月、日、年

php - 我应该如何使用 PHP 散列我的密码?

java - Spring Boot - 自定义 JSON 序列化

hash - 使用 "each"迭代哈希时添加新成员

hash - 为什么Hash函数除法只用素数