-
Notifications
You must be signed in to change notification settings - Fork 0
/
htab_erase.c
44 lines (37 loc) · 1 KB
/
htab_erase.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 2) htab_erase.c
// Ondřej Zobal (xzobal01)
// FIT VUT v Brně
// 15.4.2022
#include "htab.h"
#include "htab_t.h"
#include "htab_item_t.h"
#include <stdbool.h>
#define AVG_LEN_MIN 0.5
bool htab_erase(htab_t * t, htab_key_t key) {
// Computing hash
size_t hash = htab_hash_function(key);
// Looking up the has in the hash table
htab_item_t *items = t->arr_ptr[hash % t->arr_size];
htab_item_t *prev = NULL;
while(items != NULL) {
// If a match is found
if(!strcmp(items->pair.key, key)) {
if(prev != NULL) {
prev->next = items->next;
}
else {
t->arr_ptr[hash % t->arr_size] = NULL;
}
free((char*) items->pair.key);
free(items);
t->size--;
if (t->size / t->arr_size < AVG_LEN_MIN){
htab_resize(t, t->size/2);
}
return true;
}
prev = items;
items = items->next;
}
return false;
}