25 |
/* Add data to the end of the linked list. */ |
/* Add data to the end of the linked list. */ |
26 |
void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data) |
void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data) |
27 |
{ |
{ |
28 |
llist_t *new_item = xmalloc(sizeof(llist_t)); |
while (*list_head) |
29 |
|
list_head = &(*list_head)->link; |
30 |
new_item->data = data; |
*list_head = xzalloc(sizeof(llist_t)); |
31 |
new_item->link = NULL; |
(*list_head)->data = data; |
32 |
|
/*(*list_head)->link = NULL;*/ |
|
if (!*list_head) |
|
|
*list_head = new_item; |
|
|
else { |
|
|
llist_t *tail = *list_head; |
|
|
|
|
|
while (tail->link) |
|
|
tail = tail->link; |
|
|
tail->link = new_item; |
|
|
} |
|
33 |
} |
} |
34 |
|
|
35 |
/* Remove first element from the list and return it */ |
/* Remove first element from the list and return it */ |
36 |
void* FAST_FUNC llist_pop(llist_t **head) |
void* FAST_FUNC llist_pop(llist_t **head) |
37 |
{ |
{ |
38 |
void *data, *next; |
void *data = NULL; |
39 |
|
llist_t *temp = *head; |
|
if (!*head) |
|
|
return NULL; |
|
|
|
|
|
data = (*head)->data; |
|
|
next = (*head)->link; |
|
|
free(*head); |
|
|
*head = next; |
|
40 |
|
|
41 |
|
if (temp) { |
42 |
|
data = temp->data; |
43 |
|
*head = temp->link; |
44 |
|
free(temp); |
45 |
|
} |
46 |
return data; |
return data; |
47 |
} |
} |
48 |
|
|
49 |
/* Unlink arbitrary given element from the list */ |
/* Unlink arbitrary given element from the list */ |
50 |
void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm) |
void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm) |
51 |
{ |
{ |
52 |
llist_t *crt; |
if (!elm) |
|
|
|
|
if (!(elm && *head)) |
|
|
return; |
|
|
|
|
|
if (elm == *head) { |
|
|
*head = (*head)->link; |
|
53 |
return; |
return; |
54 |
} |
while (*head) { |
55 |
|
if (*head == elm) { |
56 |
for (crt = *head; crt; crt = crt->link) { |
*head = (*head)->link; |
57 |
if (crt->link == elm) { |
break; |
|
crt->link = elm->link; |
|
|
return; |
|
58 |
} |
} |
59 |
|
head = &(*head)->link; |
60 |
} |
} |
61 |
} |
} |
62 |
|
|
86 |
} |
} |
87 |
return rev; |
return rev; |
88 |
} |
} |
89 |
|
|
90 |
|
llist_t* FAST_FUNC llist_find_str(llist_t *list, const char *str) |
91 |
|
{ |
92 |
|
while (list) { |
93 |
|
if (strcmp(list->data, str) == 0) |
94 |
|
break; |
95 |
|
list = list->link; |
96 |
|
} |
97 |
|
return list; |
98 |
|
} |