Write a function that adds an element to a hash table in which collisions are resolved by separate chaining (insertion at the end of the list).
Difficulty level
Video recording
This exercise is mostly suitable for students
typedef int element;
typedef struct node
{
element data;
struct node * next;
} *list;
typedef list hashtable[M];
int insert_list(list *l, element nb)
{
if(*l==NULL)
{
*l=(list)malloc(sizeof(struct node));
if(!(*l)) return 0;
(*l)->data=nb;
(*l)->next=NULL;
return 1;
}
return insert_list(&((*l)->next),nb);
}
int insert(hashtable h, element nb)
{
return insert_list(&h[hash(nb)],nb);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using the radix sort algorithm