summaryrefslogtreecommitdiff
path: root/src/ll.c
blob: 21e413e7ef60f209fd22f5dff4569be630d6193e (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "ll.h"

#include <stdlib.h>
#include <error.h>
#include <errno.h>

#include <stdio.h>

// Initialize an empty nodelist object
struct nodelist* nodelist_init(struct nodelist *start) {
    if(start == NULL) {
        start = calloc(1, sizeof(struct nodelist));
        if(start == NULL) {
            error(0, errno, "Could not allocate space for empty node list object");
            return NULL;
        }
    }

    start->fullpath = NULL;
    start->next = NULL;
    
    return start;
}

struct nodelist* nodelist_prepend(struct nodelist *new, struct nodelist *list) {
    new->next = list;
    return new;
}

struct nodelist* nodelist_append(struct nodelist *list, struct nodelist *append) {
    struct nodelist *p = NULL, *prev = NULL;
    for(p = list; p != NULL; p = p->next)
        prev = p;

    // If the pointer p is null, prev will never be set, so just error out
    if(prev == NULL)
        return NULL;

    prev->next = append;

    return list;
}

int nodelist_delete(struct nodelist *list) {
    struct nodelist *p, *next;
    for(p = list; p != NULL; p = next) {    
        next = p->next;
        free(p);
    }

    return 0;
}