I am having a problem with erase function. Please help.
Search and erase implementation
void erase(string key){
int idx=hashfun(key);
Node<T>*temp=table[idx];
Node<T>*prev=NULL;
while(temp){
if(temp->key==key){
if(prev)prev->next=temp->next;
else table[idx]=temp->next;
temp->next=NULL;
delete temp;
}
prev=temp;
temp=temp->next;
}
}
steps
- find the hasvalue corresponding to given key
- find the node which we have to delete and then delete that node
to delete node you also need prev pointer (linked list Deletion concept)
it is quiet simple
i hope you understood this
if not feel free to ask