Add cfulist_delete_data

It is important to let the programmer remove a node from the linked list, as it is a very common operation.
This commit is contained in:
Diogo Cordeiro 2018-05-22 16:16:15 +01:00
parent 1294791f0c
commit b4faaca915
3 changed files with 30 additions and 0 deletions

View File

@ -509,6 +509,11 @@ cfulist_map(). The return value is used to build a new list.
Pop a value from the end of the list (removing it from the list).
@end deftypefun
@deftypefun void cfulist_delete_data (cfulist_t * @var{list}, void * @var{data})
Deletes the entry in the list associated with value.
@end deftypefun
@deftypefun {int} cfulist_unshift_data (cfulist_t * @var{list}, void * @var{data}, size_t @var{data_size})
Add a value at the beginning of the list.

View File

@ -412,6 +412,28 @@ cfulist_pop(cfulist_t *list) {
return NULL;
}
void
cfulist_delete_data(cfulist_t *list, void *data) {
cfulist_entry *ptr = NULL;
if (!list) {
return;
}
lock_list(list);
if (list->entries) {
for (ptr = list->entries; ptr && ptr->data != data; ptr = ptr->next)
;
if (ptr && ptr->data == data) {
(ptr->prev)->next = ptr->next;
free (ptr);
}
}
unlock_list(list);
}
int
cfulist_unshift(cfulist_t *list, void *data) {
return cfulist_unshift_data(list, data, 0);

View File

@ -81,6 +81,9 @@ int cfulist_push_data(cfulist_t *list, void *data, size_t data_size);
/* Pop a value from the end of the list. */
int cfulist_pop_data(cfulist_t *list, void **data, size_t *data_size);
/* Deletes the entry in the list associated with value. */
void cfulist_delete_data(cfulist_t *list, void *data);
/* Add a value at the beginning of the list. */
int cfulist_unshift_data(cfulist_t *list, void *data, size_t data_size);