简体   繁体   English

硬件:创建自己的哈希表-不确定如何在其中使用链表

[英]HW: Creating my own hashtable - Unsure of how to use a linkedlist in it

I have an assignment that I am working on where we are making our own HashTables. 我有一个作业,我正在制作我们自己的HashTables。 I went to an AI today and he said that my code was "on the right track but I should use linked lists", which I'm a little confused on what he meant. 今天我去了一家人工智能公司,他说我的代码“在正确的轨道上,但我应该使用链表”,这让我对他的意思有些困惑。

Please give me a hint on what I'm missing. 请给我有关我所缺少的提示。

class HashSeparateChaining extends HashTable {

    int size = 0;
    HashFunction hf;
    List<Integer> arrayList = new ArrayList<>();

    public HashSeparateChaining(int size, HashFunction hf) {

        size = this.size;
        hf = this.hf;

        for(int i = 0; i < size; i++) {
            arrayList.add(null);
        }

    }

    @Override
    void insert(int key) throws TableFullE {
        arrayList.set(key, key);

    }

    @Override
    void delete(int key) {
        arrayList.set(key, null);
    }

    @Override
    boolean search(int key) {
        for(int z = 0; z < arrayList.size(); z++)
            if(arrayList.get(z) == key)
                return true;

        return false;
    }

}

So does the AI mean that I should use something like this? 那么AI意味着我应该使用类似的东西吗?

List<LinkedList<Integer, String>> list = new LinkedList<>();

I'm not sure SO is a homework help site, but a quick google search turns up https://www.geeksforgeeks.org/hashing-set-2-separate-chaining/ which should give you an idea. 我不确定SO是否是家庭作业的帮助网站,但可以通过Google进行快速搜索https://www.geeksforgeeks.org/hashing-set-2-separate-chaining/ ,它应该可以给您一个想法。

Probably your underlying implementation will be an ArrayList of List s. 您的基础实现可能是ListArrayList

insert: Applying the HashFunction to the key gives you an index in the ArrayList. 插入:将HashFunction应用于该键将为您提供ArrayList中的索引。 Then add the key to the back of the list stored at that index. 然后将密钥添加到存储在该索引处的列表的后面。 (Need to clarify what it means for your table to be full so you'll know when to throw the TableFullE exception). (需要弄清楚表已满意味着什么,以便您知道何时引发TableFullE异常)。

delete: Like insert, applying your HashFunction to the key to find out what List we search. 删除:类似于插入,将您的HashFunction应用于键以找出我们要搜索的列表。 Might as well use the List 's methods to remove the item. 也可以使用List的方法删除该项目。

search: like delete, except use the List 's contains() to answer the search query 搜索:与删除类似,但使用Listcontains()回答搜索查询

Lots of possible optimizations but just try and get it working first. 许多可能的优化,但请尝试使其首先工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM