list.c 3.14 KB
Newer Older
1
/*
venaas's avatar
venaas committed
2
 * Copyright (C) 2006-2009 Stig Venaas <venaas@uninett.no>
3 4 5 6 7 8
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

9 10 11 12
#include <stdlib.h>
#include <string.h>
#include "list.h"

13 14
/* Private helper functions. */
static void list_free_helper_(struct list *list, int free_data_flag) {
15
    struct list_node *node, *next;
16 17 18

    if (!list)
	return;
19

20
    for (node = list->first; node; node = next) {
21 22
        if (free_data_flag)
            free(node->data);
23 24 25 26 27 28
	next = node->next;
	free(node);
    }
    free(list);
}

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* Public functions. */

/* allocates and initialises list structure; returns NULL if malloc fails */
struct list *list_create() {
    struct list *list = malloc(sizeof(struct list));
    if (list)
	memset(list, 0, sizeof(struct list));
    return list;
}

/* frees all memory associated with the list
   note that the data pointed at from each node is also freed
   use list_free() to free only the memory used by the list itself */
void list_destroy(struct list *list) {
    list_free_helper_(list, 1);
}

/* frees the meory used by the list itself
   note that the data pointed at from each node is not freed
   use list_destroy() to free all the data associated with the list */
void list_free(struct list *list) {
    list_free_helper_(list, 0);
}

53
/* appends entry to list; returns 1 if ok, 0 if malloc fails */
54
int list_push(struct list *list, void *data) {
55 56 57 58 59
    struct list_node *node;

    node = malloc(sizeof(struct list_node));
    if (!node)
	return 0;
60

61 62 63 64 65 66 67 68
    node->next = NULL;
    node->data = data;

    if (list->first)
	list->last->next = node;
    else
	list->first = node;
    list->last = node;
venaas's avatar
venaas committed
69 70

    list->count++;
71 72 73
    return 1;
}

74 75 76 77
/* removes first entry from list and returns data */
void *list_shift(struct list *list) {
    struct list_node *node;
    void *data;
78

venaas's avatar
venaas committed
79
    if (!list || !list->first)
80
	return NULL;
81

82 83
    node = list->first;
    list->first = node->next;
84 85
    if (!list->first)
	list->last = NULL;
86 87
    data = node->data;
    free(node);
venaas's avatar
venaas committed
88
    list->count--;
89 90 91
    return data;
}

92
/* removes all entries with matching data pointer */
93 94
void list_removedata(struct list *list, void *data) {
    struct list_node *node, *t;
95

96
    if (!list || !list->first)
97 98 99
	return;

    node = list->first;
100
    while (node->data == data) {
101 102
	list->first = node->next;
	free(node);
venaas's avatar
venaas committed
103
	list->count--;
104 105 106 107 108
	node = list->first;
	if (!node) {
	    list->last = NULL;
	    return;
	}
109 110 111 112
    }
    for (; node->next; node = node->next)
	if (node->next->data == data) {
	    t = node->next;
113
	    node->next = t->next;
114
	    free(t);
venaas's avatar
venaas committed
115
	    list->count--;
116 117 118 119
	    if (!node->next) { /* we removed the last one */
		list->last = node;
		return;
	    }
120 121 122
	}
}

123 124
/* returns first node */
struct list_node *list_first(struct list *list) {
125
    return list ? list->first : NULL;
126 127 128 129 130 131
}

/* returns the next node after the argument */
struct list_node *list_next(struct list_node *node) {
    return node->next;
}
venaas's avatar
venaas committed
132 133 134 135 136

/* returns number of nodes */
uint32_t list_count(struct list *list) {
    return list->count;
}
137 138 139 140

/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */