I have made a set ADT implementation with set_intersection being one of the functions.
It works well on integers, it goes through both testing systems it shall go through, but when I am trying to use it on chars it isn't working at all. Even though it is made with void * support.
set_t *set_intersection(set_t *a, set_t *b) {
set_t *inter = set_create(a->cmpfunc);
set_iter_t *iterA = set_createiter(a);
set_iter_t *iterB;
void *itemA = set_next(iterA);
void *itemB;
while(itemA != NULL) {
iterB = set_createiter(b);
itemB = set_next(iterB);
while (itemB != NULL) {
if (itemA == itemB) {
set_add(inter, itemA);
break;
}
itemB = set_next(iterB);
}
set_destroyiter(iterB);
itemA = set_next(iterA);
}
set_destroyiter(iterA);
list_sort(inter->list);
return inter;
}
Using this struct:
struct set {
list_t *list;
cmpfunc_t cmpfunc;
int size;
};
This is what's happening when trying to do intersect on 2 sets with characters:
Working on integers:
Aucun commentaire:
Enregistrer un commentaire