#include "linkedlist.h"

// Create empty list of size 0
// returns [ null | null ]
LinkedList* init_linkedlist() {
    LinkedList* ll = calloc(1, sizeof(LinkedList));
    return ll;
}

// Append to the end of the linked list
void append(LinkedList* ll, int data) {
    if (ll->head == NULL) {
        LinkedListNode* node = 
            calloc(1, sizeof(LinkedListNode));
        node->data = data;
        ll->head = node;
        ll->tail = node;
    } else {
        LinkedListNode* tail = ll->tail;
        LinkedListNode* new_node =
            calloc(1, sizeof(LinkedListNode));

        new_node->data = data;

        tail->next = new_node;

        new_node->prev = tail;

        ll->tail = new_node;
    }
}

int size(LinkedList* ll) {
    if (ll->head == NULL) {
        return 0;
    } else {
        LinkedListNode* head = ll->head;

        int counter = 0;
        LinkedListNode* curr = head;
        while (curr != NULL) {
            curr = curr->next;
            counter++;
        }

        return counter;
    }
}
