Line 121 https://ide.codingblocks.com/s/347003 Not working
Doubt in erase()
@girishgargcool, these are some mistakes in your code :-
- you are not handling the case when the node to be deleted is the head of the linked list , as in that case you would need to update the table[index] value
- ** important ** you need to set , toBeDeleted->next=NULL, before deleting that value, because if you don’t then destructor will delete the entire linked list starting from this node .
correct code :-
void erase(string key){
int idx = hashFunc(key);
Node<T>* temp = table[idx];
Node<T>* prev = NULL;
while(temp != NULL){
if(temp->key == key){
if(prev){
prev->next = temp->next;
}
else{
table[idx] = temp->next;
}
temp->next = NULL;
delete temp;
return ;
}
prev = temp;
temp = temp->next;
}
}
Thank you so much bro