LIST_ITEM_HANDLE singlylinkedlist_add()

in src/singlylinkedlist.c [62:103]


LIST_ITEM_HANDLE singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item)
{
    LIST_ITEM_INSTANCE* result;

    /* Codes_SRS_LIST_01_006: [If any of the arguments is NULL, singlylinkedlist_add shall not add the item to the list and return NULL.] */
    if ((list == NULL) ||
        (item == NULL))
    {
        LogError("Invalid argument (list=%p, item=%p)", list, item);
        result = NULL;
    }
    else
    {
        LIST_INSTANCE* list_instance = list;
        result = malloc(sizeof(LIST_ITEM_INSTANCE));

        if (result == NULL)
        {
            /* Codes_SRS_LIST_01_007: [If allocating the new list node fails, singlylinkedlist_add shall return NULL.] */
            /*return as is*/
        }
        else
        {
            /* Codes_SRS_LIST_01_005: [singlylinkedlist_add shall add one item to the tail of the list and on success it shall return a handle to the added item.] */
            result->next = NULL;
            result->item = item;

            if (list_instance->head == NULL)
            {
                list_instance->head = result;
                list_instance->tail = result;
            }
            else
            {
                list_instance->tail->next = result;
                list_instance->tail = result;
            }
        }
    }

    return result;
}