-
Notifications
You must be signed in to change notification settings - Fork 0
/
htab_clear.c
41 lines (33 loc) · 1.08 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
// htab_clear.c
// Řešení IJC-DU2, příklad b), 27.3.2021
// Autor: Samuel Dobroň, FIT
// Přeloženo: gcc 10.2.1
#include <stdlib.h>
#include "htab.h"
#include "htab_private.h"
/** @brief Function clears all the data from table but
* table is not reallocated.
* @param t Pointer to the table.
*/
void htab_clear(htab_t * t)
{
if (!t)
return;
for (size_t i = 0; i < t->arr_size; i++) // iterating through indexes, the same index pairs are processed by while bellow
{
if (!t->data[i])
continue;
htab_item *next_same_index_element = t->data[i]->next;
while (next_same_index_element) // remove all the elements at the same index
{
htab_item *future_same_index_element = next_same_index_element->next;
free((void *)(next_same_index_element->element.key));
free(next_same_index_element);
next_same_index_element = future_same_index_element;
}
free((void *)(t->data[i]->element.key));
free(t->data[i]);
t->data[i] = NULL;
}
t->size = 0;
}