int hashKey(int key)
    return key % tableSize;

void insertar(int key)
    int index = hashKey(key);

    while (hashTable[index] != NULL)
        hashTable[index] = key;

i'm trying to add values to this hash table array, but i"m having trouble trying to make it. I already initialized the hash table to NULL but i don't know how to add the values and validate that the space has already a value in it.

Recommended Answers

All 2 Replies

I'm afraid we'll need a lot more info. Like the rest of the hashtable class. What collision handling scheme you want to follow. You're goals in writing this class.

Hash tables are containers. Hashing creates an integer from other types, usually the characters of a key, where it represents the values and positions of those characters. One divides the hash by the bucket count to select a bucket, which is a simple container like linked list where you can search for a precise match. One item per bucket is unwise! The cost of a bucket is a pointer, or 4/8 bytes, so use lots of buckets. Mod-2 bucket counts can be mod by simple and-masking out the lower bits, and the bucket count can be doubled (linear hash table) but the misplaced copied items must be culled or tolerated. Non-mod-2 bucket counts may randomize better! Hash is faster than tree, hash generate/divide cost being less than log2 comparisons, but it is not sorted. Other containers are the skip list: a tree-linked-list hybrid, the trie (a tree with one level per key character), the array and the linked list. Each has its advantages. The linked list adds/expands cheaper. Array lists always know their size, but are expensive to add or delete intermediate values. Linked list search/delete/insert requires comparing half the items on average. Lists are often used as queues in multithreaded situations, and one needs to decide on a fixed size or expensive expansion times, anticipate locking costs, thread suspension costs.

commented: I love this. Just the thing to read to a class and then hand out the quiz. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.