From b4faaca9153ea368928389c6cc5c5a385d38fe6a Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Tue, 22 May 2018 16:16:15 +0100 Subject: [PATCH] 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. --- doc/libcfu.texi | 5 +++++ src/cfulist.c | 22 ++++++++++++++++++++++ src/cfulist.h | 3 +++ 3 files changed, 30 insertions(+) diff --git a/doc/libcfu.texi b/doc/libcfu.texi index 3b1a38d..92293df 100644 --- a/doc/libcfu.texi +++ b/doc/libcfu.texi @@ -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. diff --git a/src/cfulist.c b/src/cfulist.c index 052d515..a55c41f 100644 --- a/src/cfulist.c +++ b/src/cfulist.c @@ -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); diff --git a/src/cfulist.h b/src/cfulist.h index 52e92ec..f0d5c33 100644 --- a/src/cfulist.h +++ b/src/cfulist.h @@ -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);