summaryrefslogtreecommitdiffstats
path: root/lib/linkedlist.c
blob: 6cc640396214fc11194adf3b51244c9d0f9cb338 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "linkedlist.h"

void
sll_new(struct SLinkedList *list, size_t obj_size)
{
    list->obj_size = obj_size;
    list->count = 0;
    list->head = list->tail = NULL;
};

void
sll_push(struct SLinkedList *list, void *data)
{
    struct SLLNode *node = (struct SLLNode*)data;
    if(list->tail != NULL) {
        list->tail->next = node;
    }
    if(list->head == NULL)
        list->head = node;
    list->tail = node;
    list->count++;
}

void*
sll_get(struct SLinkedList *list, size_t index)
{
    struct SLLNode *node = list->head;
    if(node == NULL) return NULL;
    if(index > list->count) return list->tail;
    for(size_t i = 0; i < index; i++) {
        node = node->next;
    }
    return node;
}