void erase(string key){
int idx = hash(key) ;
Node* temp = table[idx] ;
while(temp != NULL){
if(temp->key == key){
delete temp ;
}
temp = temp->next ;
}
}
Error in the erase fun of hashing
you have to check for the case when head of the list is the element to be deleted
refer this code :-
void erase(string key){
int idx=hashfn(key);
node*ptr=table[idx];
if(ptr->key==key){
table[idx]=ptr->next;
ptr->next = NULL;
delete (ptr);
curr_size--;
return;
}
while(ptr->next!=NULL){
if(ptr->next->key==key){
node*temp=ptr->next;
ptr->next=temp->next;
temp->next = NULL;
delete (temp);
return;
}
ptr=ptr->next;
}
return;
}
why u have used the cur_size-- only in the if satement not in the while loo[
yeah we need to decrement curr_size inside while loop when we find node to be deleted
void erase(string key){
int idx=hashfn(key);
node*ptr=table[idx];
if(ptr->key==key){
table[idx]=ptr->next;
ptr->next = NULL;
delete (ptr);
curr_size--;
return;
}
while(ptr->next!=NULL){
if(ptr->next->key==key){
node*temp=ptr->next;
ptr->next=temp->next;
temp->next = NULL;
delete (temp);
curr_size--;
return;
}
ptr=ptr->next;
}
return;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.