M
merrittr
Hi
in the code below in the main the hash table from K&R example replaces
a node when a collision occurs
What I am trying to do is set it up to "chain" another structure
beside the node that it collides with
can you spot what I am doing wrong? zork and 122 are to 2 that collide
so the "zork" node gets overwritten by
121.
#include <stdio.h>
#include <stdlib.h>
#define HASHSZ 10
struct nlist
{
struct nlist *next;
char *name;
char *defn;
};
static struct nlist *hashtab[HASHSZ] = {NULL};
char *strdup(char *);
int hash(char *str)
{
unsigned long h;
unsigned char *p;
p = (unsigned char*)str;
for(h = 0; *p != '\0'; p++)
h = 37 * h + *p;
printf("%s hash is > %d\n", str,h % HASHSZ);
return h % HASHSZ;
}
struct nlist *lookup(char *s)
{
struct nlist *np;
for (np = hashtab[hash(s)]; np != NULL; np = np->next)
{
printf("%s = %s\n",s,np->name);
if(strcmp(s, np->name) == 0)
{
return np;
}
}
return NULL;
}
struct nlist *install(char *name, char *defn)
{
struct nlist *np;
unsigned hashval;
if ((np = lookup(name)) == NULL)
{
np = (struct nlist *) malloc(sizeof(*np));
if (np == NULL || (np->name = strdup(name)) == NULL)
return NULL;
hashval = hash(name);
np->next = hashtab[hashval];
hashtab[hashval] = np;
}
else
free((void *) np->defn);
if ((np->defn = strdup(defn)) == NULL)
return NULL;
return np;
}
void printhash()
{
int i;
struct nlist *np;
for(i = 0; i < HASHSZ; i++)
{
printf("\n%d- ",i);
if (hashtab != NULL)
{
np = hashtab;
printf ("name :%s defn :%s \n",np->name, np->defn);
if (np->next != NULL)
{
np = np->next;
}
}
}
}
main(int argc, char **argv)
{
printf(" %s hashes too > %d\n",argv[1],hash(argv[1]));
install("gleep","glorp");
install("zork","zero"); //this hashes to 0
install("glorp","gleep");
install("121", "ewewfgre1"); //this hashes to 0
printhash();
printf("\ndone\n");
}
in the code below in the main the hash table from K&R example replaces
a node when a collision occurs
What I am trying to do is set it up to "chain" another structure
beside the node that it collides with
can you spot what I am doing wrong? zork and 122 are to 2 that collide
so the "zork" node gets overwritten by
121.
#include <stdio.h>
#include <stdlib.h>
#define HASHSZ 10
struct nlist
{
struct nlist *next;
char *name;
char *defn;
};
static struct nlist *hashtab[HASHSZ] = {NULL};
char *strdup(char *);
int hash(char *str)
{
unsigned long h;
unsigned char *p;
p = (unsigned char*)str;
for(h = 0; *p != '\0'; p++)
h = 37 * h + *p;
printf("%s hash is > %d\n", str,h % HASHSZ);
return h % HASHSZ;
}
struct nlist *lookup(char *s)
{
struct nlist *np;
for (np = hashtab[hash(s)]; np != NULL; np = np->next)
{
printf("%s = %s\n",s,np->name);
if(strcmp(s, np->name) == 0)
{
return np;
}
}
return NULL;
}
struct nlist *install(char *name, char *defn)
{
struct nlist *np;
unsigned hashval;
if ((np = lookup(name)) == NULL)
{
np = (struct nlist *) malloc(sizeof(*np));
if (np == NULL || (np->name = strdup(name)) == NULL)
return NULL;
hashval = hash(name);
np->next = hashtab[hashval];
hashtab[hashval] = np;
}
else
free((void *) np->defn);
if ((np->defn = strdup(defn)) == NULL)
return NULL;
return np;
}
void printhash()
{
int i;
struct nlist *np;
for(i = 0; i < HASHSZ; i++)
{
printf("\n%d- ",i);
if (hashtab != NULL)
{
np = hashtab;
printf ("name :%s defn :%s \n",np->name, np->defn);
if (np->next != NULL)
{
np = np->next;
}
}
}
}
main(int argc, char **argv)
{
printf(" %s hashes too > %d\n",argv[1],hash(argv[1]));
install("gleep","glorp");
install("zork","zero"); //this hashes to 0
install("glorp","gleep");
install("121", "ewewfgre1"); //this hashes to 0
printhash();
printf("\ndone\n");
}