summaryrefslogtreecommitdiff
path: root/sorted_list.c
blob: 478b019f1ef5997841410c7464314cfcfd1a8204 (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
53
54
55
56
/*
 * sorted_list.c:
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include "sorted_list.h"
#include "iftop.h"


void sorted_list_insert(sorted_list_type* list, void* item) {
    sorted_list_node *node, *p;

    p = &(list->root);

    while(p->next != NULL && list->compare(item, p->next->data) > 0) {
        p = p->next;
    } 

    node = xmalloc(sizeof *node);

    node->next = p->next;
    node->data = item;
    p->next = node;
}


sorted_list_node* sorted_list_next_item(sorted_list_type* list, sorted_list_node* prev) {
    if(prev == NULL) {
        return list->root.next;
    }
    else {
        return prev->next;
    }
}

void sorted_list_destroy(sorted_list_type* list) {
    sorted_list_node *p, *n;
    p = list->root.next;

    while(p != NULL) {
        n = p->next;
        free(p);
        p = n;
    }

    list->root.next = NULL;
}

void sorted_list_initialise(sorted_list_type* list) {
    list->root.next = NULL;
}