int hash(char *s) { int res=44923, i, n=strlen(s); for (i=0; i<n; i+=1) res=9*res*res+66177*res+s[i]; return (abs(res)%hashsize); }Such a function is guaranteed to produce a result in the range 0 to (hashsize-1). If we had database records that look like this:
struct person { char name[20]; int socsec; char address[50]; char phone[10]; }; | create table person ( name: char(20), socsec: int, address: char(50), phone: char(10) ) |
class HashIndex { protected: struct person *index[hashsize]; public: HashIndex() { for (int i=0; i<hashsize; i+=1) { index[i]=NULL; } } struct person *lookup(char *key) { int h=hash(key); return (index[h]); } void insert(struct person *record) { int h=hash(record->name); index[h]=record; } }; |
36.7% | of the linked lists will have | 0 | data record pointers |
36.7% | - " - | 1 | - " - |
18.4% | - " - | 2 | - " - |
6.1% | - " - | 3 | - " - |
1.5% | - " - | 4 | - " - |
0.36% | - " - | more than 4 | - " - |