Blame view

sorted_list.c 946 Bytes
pdw committed
1 2 3 4 5 6 7 8
/*
 * sorted_list.c:
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include "sorted_list.h"
chris committed
9
#include "iftop.h"
pdw committed
10 11 12 13 14 15 16 17 18 19 20


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;
    } 

chris committed
21
    node = xmalloc(sizeof *node);
pdw committed
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

    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;
}