简体   繁体   中英

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. 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?

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.

Probably your underlying implementation will be an ArrayList of List s.

insert: Applying the HashFunction to the key gives you an index in the 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).

delete: Like insert, applying your HashFunction to the key to find out what List we search. Might as well use the List 's methods to remove the item.

search: like delete, except use the List 's contains() to answer the search query

Lots of possible optimizations but just try and get it working first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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