2 * Copyright (C) 2006-2009 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;
57 /* removes first entry from list and returns data */
58 void *list_shift(struct list *list) {
59 struct list_node *node;
62 if (!list || !list->first)
66 list->first = node->next;
75 /* removes all entries with matching data pointer */
76 void list_removedata(struct list *list, void *data) {
77 struct list_node *node, *t;
79 if (!list || !list->first)
83 while (node->data == data) {
84 list->first = node->next;
93 for (; node->next; node = node->next)
94 if (node->next->data == data) {
99 if (!node->next) { /* we removed the last one */
106 /* returns first node */
107 struct list_node *list_first(struct list *list) {
108 return list ? list->first : NULL;
111 /* returns the next node after the argument */
112 struct list_node *list_next(struct list_node *node) {
116 /* returns number of nodes */
117 uint32_t list_count(struct list *list) {
121 /* Local Variables: */
122 /* c-file-style: "stroustrup" */