Im a bit stuck on how to delete skiplists without leaking any memory. Here is my skiplist definition:
typedef struct s_LinkedElement {
int value;
int nbpt;
struct s_LinkedElement **previous;
struct s_LinkedElement **next;
} LinkedElement;
struct s_Skiplist {
RNG sk_proba;
int nbLevels;
LinkedElement *sentinel;
int size;
};
My initialisation of an empty skiplist:
SkipList skiplist_create(int nblevels) {
SkipList l = malloc(sizeof(struct s_SkipList));
l->sentinel=malloc(sizeof(struct s_LinkedElement));
l->sentinel->next = malloc(sizeof(struct s_LinkedElement)*nblevels);
l->sentinel->previous = malloc(sizeof(struct s_LinkedElement)*nblevels);
for (int i = 0; i < nbLevels; i++) {
l -> sentinel -> previous[i] = l -> sentinel;
l -> sentinel -> next[i] = l -> sentinel;
}
l->size = 0;
return l;
}
To delete them, I thought about iterating through the sentinel and the next and previous tables and freeing all of the elements individually, is there any better way to do it? Is mine even valid?