user7437907
user7437907

Reputation:

Hashtable delete method

My hashtable uses this structs:

typedef struct bucket{
    user *info;
    bool available;
}bucket;

typedef struct user{
    char nick[6];
    int age;
}user;

typedef struct hashtable{
    int size;
    int elements;
    bucket **buckets;
}hashtable;

I made this delete function for my hashtable:

void delete_item(hashtable *HashTable, char *nick){
    int position = contains(HashTable, nick);
    if(position != -1){
        HashTable->buckets[position]->available = true;
        HashTable->buckets[position] = NULL;
        HashTable->elements--;
    }
}

The function itself doesnt raise an error, but if i delete an item if i try to print the hashtable the deleted position gives a segmentation fault.

The idea for delete is to set the struct in the bucket to NULL and set that bucket as available to be inserted again, and also works as a marker to the contains function to keep looking in the next position.

Upvotes: 0

Views: 78

Answers (1)

Attie
Attie

Reputation: 6979

I suspect this is your problem:

HashTable->buckets[position] = NULL;

Did you mean this?

HashTable->buckets[position]->info = NULL

Upvotes: 1

Related Questions