2 * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
13 /* allocates and initialises list structure; returns NULL if malloc fails */
14 struct list *list_create() {
15 struct list *list = malloc(sizeof(struct list));
17 memset(list, 0, sizeof(struct list));
21 /* frees all memory associated with the list */
22 void list_destroy(struct list *list) {
23 struct list_node *node, *next;
28 for (node = list->first; node; node = next) {
36 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
37 int list_push(struct list *list, void *data) {
38 struct list_node *node;
40 node = malloc(sizeof(struct list_node));
48 list->last->next = node;
56 /* removes first entry from list and returns data */
57 void *list_shift(struct list *list) {
58 struct list_node *node;
61 if (!list || !list->first)
65 list->first = node->next;
74 /* removes all entries with matching data pointer */
75 void list_removedata(struct list *list, void *data) {
76 struct list_node *node, *t;
78 if (!list || !list->first)
82 while (node->data == data) {
83 list->first = node->next;
91 for (; node->next; node = node->next)
92 if (node->next->data == data) {
96 if (!node->next) { /* we removed the last one */
103 /* returns first node */
104 struct list_node *list_first(struct list *list) {
105 return list ? list->first : NULL;
108 /* returns the next node after the argument */
109 struct list_node *list_next(struct list_node *node) {