/ Published in: C

Implement a hash table for strings
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include<stdio.h> #include<stdlib.h> #include<string.h> #define NHASH 29989 //Use a prime number! #define MULT 31 struct node { char *word; int count; struct node * next; } node; typedef struct node Node; Node *bin[NHASH]; unsigned int hash(char *p) { unsigned int h = 0; for(; *p; p++) h = MULT * h + *p; return h % NHASH; } void incword(char *s) { Node * p; int h = hash(s); for(p = bin[h]; p!= NULL; p = p->next) { { (p->count)++; return; } } if(!p) return; p->count = 1; p->next = bin[h]; bin[h] = p; } int main() { char buf[100]; for (int i=0; i<NHASH; i++) bin[i] = NULL; incword(buf); for (int i = 0; i < NHASH; i++) for (Node * p = bin[i]; p != NULL; p = p->next) return 0; }
Comments
