Projects/1BIT/summer-semester/IJC-2/htab_erase.c
2026-04-14 19:28:46 +02:00

30 lines
No EOL
790 B
C

// htab_erase.c
// Řešení IJC-DU2, příklad b), 22.4.2024
// Autor: Roman Nečas, FIT
// Přeloženo: gcc 11.4.0
#include <stdlib.h>
#include <string.h>
#include "htab.h"
#include "htab_struct.h"
bool htab_erase(htab_t *t, htab_key_t key) {
size_t idx = htab_hash_function(key) % t->arr_size;
struct htab_item *item = t->arr_ptr[idx];
struct htab_item *prev = NULL;
while (item != NULL) {
if (strcmp(item->pair.key, key) == 0) {
if (prev == NULL)
t->arr_ptr[idx] = item->next;
else
prev->next = item->next;
free((void *)item->pair.key);
free(item);
t->size--;
return true;
}
prev = item;
item = item->next;
}
return false;
}