From 90643d82470dba0c5aaf884a231a55894b8dd39f Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Thu, 21 Nov 2024 17:20:47 +0100 Subject: [PATCH] created why2_list_remove_at in llist removes element by index --- include/llist.h | 1 + src/core/lib/utils/llist.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/llist.h b/include/llist.h index ec7dea1..2d135b6 100644 --- a/include/llist.h +++ b/include/llist.h @@ -42,6 +42,7 @@ typedef struct void why2_list_push(why2_list_t *list, void *value, unsigned long size); //PUSH ELEMENT TO LIST BACK void why2_list_push_at(why2_list_t *list, unsigned long index, void *value, unsigned long size); //PUSH ELEMENT TO INDEX index of LIST void why2_list_remove(why2_list_t *list, why2_node_t *node); //REMOVE ELEMENT +void why2_list_remove_at(why2_list_t *list, unsigned long index); //REMOVE ELEMENT WITH INDEX index void why2_list_remove_back(why2_list_t *list); //REMOVE LAST ELEMENT why2_node_t *why2_list_find(why2_list_t *list, void *value); //FIND ELEMENT IN LIST unsigned long why2_list_get_size(why2_list_t *list); //GET SIZE diff --git a/src/core/lib/utils/llist.c b/src/core/lib/utils/llist.c index e5c72b8..cbda4bc 100644 --- a/src/core/lib/utils/llist.c +++ b/src/core/lib/utils/llist.c @@ -117,6 +117,29 @@ void why2_list_remove(why2_list_t *list, why2_node_t *node) why2_deallocate(node); } +void why2_list_remove_at(why2_list_t *list, unsigned long index) +{ + if (list == NULL) return; //EMPTY LIST + + why2_node_t *node_to_remove; + + if (index != 0) //SHOULDN'T BE FIRST + { + why2_node_t *node_before = list -> head; + for (unsigned long j = 0; j < index - 1; j++) node_before = node_before -> next; //SCROLL TO THE POSITION + + node_to_remove = node_before -> next; + node_before -> next = node_to_remove -> next; + } else //ADD BEFORE THE WHOLE LIST + { + node_to_remove = list -> head; + list -> head = node_to_remove -> next; + } + + why2_deallocate(node_to_remove -> value); + why2_deallocate(node_to_remove); +} + void why2_list_remove_back(why2_list_t *list) { why2_node_t *head = list -> head;