This program is driving my crazy!
Ok, so I need to use separate chaining hash in this program.
This is my function that is suppose to add a new hash after the data has been added to the linked list.
Code:
typedef struct node {
data list;
struct node *next;
} Node;
typedef struct hashNode {
Node *link;
struct hashNode *next;
} hNode;
Node* hashtab[13];
int add(char* name, char* number) {
int i = 0;
Node* n;
if ((n = lookup(name)) == NULL) {
i = hash(name);
n = (Node*) malloc(sizeof(Node));
if (n == NULL) return 0;
strcpy(n->list.name, name);
if (n->list.name == NULL) return 0;
n->next = hashtab[i];
hashtab[i] = n;
}
else
free(n->list.number);
strcpy(n->list.number, number);
if(n->list.number == NULL) return 0;
return 1;
}
Node* lookup(char *s) {
int i = hash(s);
Node *n = 0;
n = hashtab[i]->link;
for( ; n != NULL ; n = n->next) {
if (!strcasecmp(n->list.name, s))
return n;
}
return NULL;
}
What I'm doing wrong is in my add function, I create a Node, which contains the call
data. I copy name and number into that Node, instead of using a DIFFERENT struct for
the hash bucket list, because those nodes should POINT TO a linked list
node, instead of containing the data.
I think hashtab[13] should be hNode* instead right? IDK, I'm confused and don't know what to change.
Thanks in advance!