when I'm adding to a NULL bucket in a hashTable, my function works very well, but it doesn't when there is already a value in that bucket, and I can't find the problem here !?
void myHashSetAdd(MyHashSet* obj, int key) {
int bucket=key%10;
ListNode* test=obj->t[bucket],*a;
if(!test){ // if bucket is empty ( works well )
obj->t[bucket]=node(key);
} else { // if it's full ( here is the problem )
a=node(key);
a->next=test; // assign it as the head of the linked list
obj->t[bucket]=a;
}
}
Here is My hashTable fucntions:
typedef struct {
int val;
struct ListNode *next;
}ListNode;
typedef struct {
ListNode *t[10];
} MyHashSet;
ListNode *node(int val){
ListNode *temp=malloc(sizeof(ListNode));
temp->val=val,temp->next=0;
return temp;
}
MyHashSet* myHashSetCreate() {
MyHashSet *hashTable=malloc(sizeof(MyHashSet));
for(int i=0;i<10;i++){hashTable->t[i]=0;} // all buckets to NULL;
return hashTable;
}