data_structures

data structures in C
Log | Files | Refs | Feed

commit 625911257a550014862d6c3619e3e8bf562c53af
parent 7ac9b49a4438693a9473b5d0e73a3e5809afb177
Author: Jenny Doe <tng@soykaf.me>
Date:   Fri, 22 Mar 2019 16:15:41 +0100

fixed: sets consistency and set_remove()

Diffstat:
Mset.c | 14++++++++------
Mset_test.c | 10++++++++--
2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/set.c b/set.c @@ -41,14 +41,16 @@ set_remove(Set * set, void **data) return (1); SetElmt *t = set->set->head; - if (set->match(t->data, *data)) { + if (set->match(t->data, *data) == 0) return list_rem_next(set->set, NULL, data); - } - while ((t = t->next) != NULL) { - if (set->match(t->next->data, *data)) + do { + if (t->next == NULL) /* reached tail */ + break; + + if (set->match(t->next->data, *data) == 0) return list_rem_next(set->set, t, data); - } + } while ((t = t->next) != NULL); return (1); } @@ -56,7 +58,7 @@ set_remove(Set * set, void **data) int set_is_member(Set * set, const void *data) { - if (set->set->size == 0) /* empty list */ + if (set->set->size <= 0) /* empty list */ return (1); SetElmt *t = set->set->head; diff --git a/set_test.c b/set_test.c @@ -13,7 +13,9 @@ char_match(const void *c1, const void *c2) int main(void) { - char t[10] = "abc"; + char t[10] = "abc"; + char v = 'z'; + char *u = &v; Set *my_set = malloc(sizeof(Set)); if (my_set == NULL) { @@ -30,16 +32,20 @@ main(void) set_init(my_set, char_match, NULL); set_insert(my_set, &t[0]); /* push 'a' */ - list_ins_next(my_set->set, NULL, &t[0]); set_insert(my_set, &t[1]); /* push 'b' */ set_insert(my_set, &t[2]); /* push 'c' */ set_insert(my_set, &t[0]); /* push 'a' */ + set_insert(my_set, &v); /* push 'z' */ + set_remove(my_set, (void **)&u); /* remove 'z' */ + + /* easier than checking return value */ if (my_set->set->size != 3) { fprintf(stderr, "Failed to insert some element in my_set!\n"); + fprintf(stderr, "%d\n", my_set->set->size); return (1); }