int singlylinkedlist_remove()

in src/singlylinkedlist.c [105:162]


int singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item)
{
    int result;

    /* Codes_SRS_LIST_01_024: [If any of the arguments list or item_handle is NULL, singlylinkedlist_remove shall fail and return a non-zero value.] */
    if ((list == NULL) ||
        (item == NULL))
    {
        LogError("Invalid argument (list=%p, item=%p)", list, item);
        result = MU_FAILURE;
    }
    else
    {
        LIST_INSTANCE* list_instance = list;
        LIST_ITEM_INSTANCE* current_item = list_instance->head;
        LIST_ITEM_INSTANCE* previous_item = NULL;

        while (current_item != NULL)
        {
            if (current_item == item)
            {
                if (previous_item != NULL)
                {
                    previous_item->next = current_item->next;
                }
                else
                {
                    list_instance->head = current_item->next;
                }

                if (current_item == list_instance->tail)
                {
                    list_instance->tail = previous_item;
                }

                free(current_item);

                break;
            }

            previous_item = current_item;
            current_item = current_item->next;
        }

        if (current_item == NULL)
        {
            /* Codes_SRS_LIST_01_025: [If the item item_handle is not found in the list, then singlylinkedlist_remove shall fail and return a non-zero value.] */
            result = MU_FAILURE;
        }
        else
        {
            /* Codes_SRS_LIST_01_023: [singlylinkedlist_remove shall remove a list item from the list and on success it shall return 0.] */
            result = 0;
        }
    }

    return result;
}