1 /* Copyright (c) 2006-2010, UNINETT AS
2 * Copyright (c) 2010-2012, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
9 /* allocates and initialises list structure; returns NULL if malloc fails */
10 struct list *list_create() {
11 struct list *list = malloc(sizeof(struct list));
13 memset(list, 0, sizeof(struct list));
17 /* frees all memory associated with the list */
18 void list_destroy(struct list *list) {
19 struct list_node *node, *next;
24 for (node = list->first; node; node = next) {
32 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
33 int list_push(struct list *list, void *data) {
34 struct list_node *node;
36 node = malloc(sizeof(struct list_node));
44 list->last->next = node;
53 /* removes first entry from list and returns data */
54 void *list_shift(struct list *list) {
55 struct list_node *node;
58 if (!list || !list->first)
62 list->first = node->next;
71 /* removes all entries with matching data pointer */
72 void list_removedata(struct list *list, void *data) {
73 struct list_node *node, *t;
75 if (!list || !list->first)
79 while (node->data == data) {
80 list->first = node->next;
89 for (; node->next; node = node->next)
90 if (node->next->data == data) {
95 if (!node->next) { /* we removed the last one */
102 /* returns first node */
103 struct list_node *list_first(struct list *list) {
104 return list ? list->first : NULL;
107 /* returns the next node after the argument */
108 struct list_node *list_next(struct list_node *node) {
112 /* returns number of nodes */
113 uint32_t list_count(struct list *list) {
117 /* Local Variables: */
118 /* c-file-style: "stroustrup" */