Write a function that adds an element of type (char *) to a hash table in which collisions are resolved by applying the hash coalesced without separated zones.


Difficulty level
Video recording
This exercise is mostly suitable for students
int add_coalesced(hashtable T, int m, element e)
{	
	int r= m-1, i = hash_function(e), j ; 
	if (empty(T,i))
	{
		strcpy(T[i].data , e) ;
		T[i].link = -1 ; 
		return 1 ;
	}

	if (hash_function(T[i].data) == i)  
	{
		while (T[i].link != -1 && strcmp(T[i]. data ,e))  
	               i = T[i].link ;
         	if (strcmp(T[i].data , e)==0) return 0;
	     	while (r >= 0 && ! empty (T,r)) r--;
	     	if (r < 0) return 0 ; 
         	strcpy(T[r].data , e) ; 
         	T[r].link = -1 ; 
         	T[i].link = r ; 
         	return 1 ;
	}

	while (r >= 0 && !empty(T,r)) r--;
	if (r < 0) return 0 ;
	T[r] = T[i];
	j = hash_function(T[i].data) ;
	while (T[j].link != i ) 
		j = T[j]. link;
	T[j].link = r ; 
    	strcpy(T[i].data , e) ; 
    	T[i].link = -1 ; 
    	return 1 ;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
UVA 10009 - All Roads Lead Where