int SLInsert(SortedListPtr list, void *newObj) { node *p = list->base->next; // find the first item larger or equal to newObj while (p != list->base) { if (list->CF(newObj, p->data) != -1) { break; } p = p->next; } node *tmp = malloc(sizeof(node)); if (tmp == NULL) return 0; // initialize a node tmp->refByItrCount = 0; tmp->data = newObj; // insert a node tmp->prev = p->prev; p->prev->next = tmp; tmp->next = p; p->prev = tmp; return 1; }
int SLRemove(SortedListPtr list, void *newObj) { node *p = list->base->next; // find the first item equal to newObj while (p != list->base) { if (list->CF(newObj, p->data) == 0) { break; } p = p->next; } // if not find an item equal to newObj if (p == list->base) return 0; p->next->prev = p->prev; p->prev->next = p->next; //if there is no iterator "pointing" to this item, free it. if (p->refByItrCount == 0) { list->DF(p->data); free(p); } p->next = NULL; p->prev = NULL; return 1; }