Beispiel #1
0
void list_remove(linked_list list, const int index){
    struct list_node* n;    // The node to remove

    // Handle head case
    if(index == 0 && list->size > 0){
        n = list->head;
        list->head = list->head->next;
    } else {
        n = get_node_at(list, index-1);
        // Handle tail case
        if(index == list->size - 1){
            list->tail = n;
            n = list->tail->next;
            list->tail->next = NULL;
        } else {
            // Handle normal case, link previous with following node
            struct list_node* to_remove = n->next;
            n->next = to_remove->next;
            n = to_remove;
        }
    }

    // Custom free
    if(list->fun != NULL)
        list->fun(n->elem);
    // Free node and its element
    free(n->elem);
    free(n);

    // Decrease size
    list->size--;
}
Beispiel #2
0
void list_delete(linked_list list){
    if(list == NULL)
        return;

    // Itearate through each node and free its memory
    struct list_node* next = list->head;
    while(next != NULL){
        struct list_node* temp = next->next;
        // Custom free
        if(list->fun != NULL)
            list->fun(next->elem);

        free(next->elem);
        free(next);
        next = temp;
    }

    // Free list struct memory
    free(list);
}