int SLRemove(SortedListPtr list, void *newObj) { SortedNodePtr current = list->head, previous = NULL; while(current->link != NULL && list->compare(current->value, newObj) != 0) { // Store current node into previous previous = current; // Move to next node current = current->link; } // Only remove if count on node is one or lower if(previous == NULL && current->count <= 1) { list->head = current->link; list->destruct(current->value); free(current); // Decrement list size list->counter--; return 1; } else if(current->count <= 1) { // Previous points to node after the one being deleted previous->link = current->link; // Free the memory list->destruct(current->value); free(current); // Decrement list size list->counter--; return 1; } else { // Make reference count go down by one current->count--; } // Couldn't remove return 0; }
void SLDestroy(SortedListPtr list) { // Traverse to end up list while deallocating memory while(list->head != NULL) { // TempNode points to current and current points to next node SortedNodePtr tempNode = list->head; list->head = tempNode->link; // Free data if need be list->destruct(tempNode->value); // Free the node free(tempNode); } // Free everything else in list free(list->head); free(list); }