I am working on a practice problem, involving designing a Direct address table, with possibly non-distinct keys. The constraint is that INSERT, DELETE, and SEARCH should run in O(1) time; arguments being the pointer to set objects.
One obvious solution is to use chaining, with the table entry pointing to the head of the linked list (which could be NULL). With such chaining INSERT and DELETE would sure run in O(1) time, however, SEARCH would not…
Any suggestions would be appreciated.
Study the design of the STL associative containers std::unordered_set, std:unordered_map, std::unordered_multiset, std::unorderd_multimap depending on whether you want to store {unique, non-unique} and {keys, key-values}. If you don’t have a C++11 compiler (e.g. MSVC++ >= 2010 or gcc >= 4.4) you could use Boost.Unordered.
UPDATE:
If you are specifically looking for a C library: take a look at http://attractivechaos.wordpress.com/2008/09/02/implementing-generic-hash-library-in-c/