-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtab_clear.c
60 lines (55 loc) · 2.15 KB
/
htab_clear.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// htab_clear.c
// Řešení IJC-DU2, příklad b), 19.4.2022
// Autor: Richard Kocián, FIT
// Přeloženo: gcc 9.4.0
#include "htab.h"
#include "htab_struct.h"
#include <stdlib.h>
bool eraseItemWithoutResize(htab_t *t, htab_key_t key);
void htab_clear(htab_t *t) {
if (t != NULL) {
for (unsigned i = 0; i < t->arr_size; i++) {
if (t->list[i] != NULL) {
struct htab_item *currentItem = t->list[i];
while (currentItem->next != NULL) {
struct htab_item *nextItem = currentItem->next;
eraseItemWithoutResize(t, currentItem->htabPair->key);
currentItem = nextItem;
}
eraseItemWithoutResize(t, t->list[i]->htabPair->key);
}
}
}
}
bool eraseItemWithoutResize(htab_t *t, htab_key_t key) {
if (t != NULL) {
for (unsigned i = 0; i < t->arr_size; ++i) {
if (t->list[i] != NULL) {
if (strcmp(t->list[i]->htabPair->key,key) == 0) { // odebrání položky v seznamu ukazatelů na záznamy
struct htab_item *nexItem = t->list[i]->next;
free((void *) t->list[i]->htabPair->key);
free(t->list[i]->htabPair);
free(t->list[i]);
t->list[i] = nexItem;
t->size--;
return true;
}
struct htab_item *currentItem = t->list[i];
while (currentItem->next != NULL) {
if (strcmp(currentItem->next->htabPair->key, key) == 0) { // odebrání záznamu
struct htab_item *nextItem = currentItem->next->next;
free((void *) currentItem->next->htabPair->key);
free(currentItem->next->htabPair);
free(currentItem->next);
currentItem->next = nextItem;
t->size--;
return true;
}
currentItem = currentItem->next;
}
}
}
return false;
}
return false;
}