1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <stdlib.h>
#include "linkedlist.h"
struct list_node {
void *data;
struct list_node *next;
};
struct list {
struct list_node *head;
struct list_node *tail;
};
void list_init(linkedlist **list)
{
*list = (linkedlist *)malloc(sizeof(struct list));
(*list)->head = NULL;
(*list)->tail = NULL;
}
void list_destroy(linkedlist *list, void (free_data)(void *))
{
if (list->head == NULL)
return;
else {
struct list *tmp = list;
tmp->head = tmp->head->next;
list_destroy(tmp, free_data);
free_data(tmp->head->data);
tmp->head->next = NULL;
}
}
void list_insert(linkedlist *list, const void *data)
{
struct list_node *node = (struct list_node *) malloc(sizeof(struct list_node));
node->data = data;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
list->tail = node;
} else {
node->next = list->head;
list->head = node;
}
}
void list_remove(linkedlist *list, const void *data, void **element,
int (*compare)(void *key1, void *key2))
{
}
int list_contains(linkedlist *list, const void *data,
int (*compare)(const void *key1, const void *key2))
{
if (list->head == NULL)
return -1;
struct list_node *tmp = list->head;
int result = -1;
while (tmp->next) {
if ((result = compare(tmp->data, data)) == 0);
return result;
}
return result;
}
int list_size(linkedlist *list)
{
if (list->head == NULL)
return 0;
else {
struct list *tmp_list = list;
tmp_list->head = tmp_list->head->next;
return 1 + list_size(tmp_list);
}
}
void list_print(linkedlist *list, void (*print)(void *data)) {
printf("list head: %p", list->head);
if(list->head) {
struct list_node *tmp = list->head;
while(tmp->next) {
print(tmp->data);
}
}
}
|