Hashtable Implementation-Erase Key

Kindly check whether my code for erasing a key from the hashmap is correct or no.
LINK : https://ide.codingblocks.com/s/330038

@gptradhika1208, you need to break out of the loop when (temp->key == key) after deleting temp as,while loop is still continuing so after deleting temp, running these two lines
prev = temp;
temp = temp->next;
will raise runtime error, as we have deleted temp

I did understand what you wanted to say. But I’m still confused as to how to correct the code. Could you please modify my code given in the above link leaving a comment?

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;
    }
}

just one very small change, you just have to return after deleting temp that’s all ,

Yes,now I understood what you were trying to explain. Thankyou