#include "simple.h"

simple_node_t *head = NULL;

void simple_add(void *item)
{
    if (item != NULL)
    {
        TRACE(printf("head@%p: adding node %p, ", head, item));
        // malloc new node and set pointers
        simple_node_t *new_node = malloc(sizeof(simple_node_t));
#if VERIFY
        assert(new_node != NULL);
#endif
        new_node->item = item;
        new_node->next = head;
        head = new_node;
        TRACE(printf("newhead@%p\n", head));
    }
}

// prints the whole list
void simple_print_list()
{
    simple_node_t *current = head;
    TRACE(printf("head@%p\n", head));

    while (current != NULL)
    {
        printf("%d, ", *(int *)current->item);
        current = current->next;
    }
    TRACE(printf("\n"));
}

void *simple_try_remove_any()
{
    void *item = NULL;

    // if list is not empty, remove first node and return item
    if (head != NULL)
    {
        TRACE(printf("head@%p: removing node ", head));
        simple_node_t *temp = head;
        head = (head)->next;
        item = temp->item;
        free(temp);
        TRACE(printf("%p, newhead@%p\n", item, head));
    }

    return item;
}