'why can't I add to my linked list in my hash table if it's already full in C?
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;
}
Solution 1:[1]
You can achieve this using hasManyThrough relationship. After defining relationship you can count simply using count() on relationship.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohammad Mirsafaei |
