created why2_list_remove_at in llist

removes element by index
This commit is contained in:
Václav Šmejkal 2024-11-21 17:20:47 +01:00
parent 38f814ac9c
commit 90643d8247
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
2 changed files with 24 additions and 0 deletions

View File

@ -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(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_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(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 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 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 unsigned long why2_list_get_size(why2_list_t *list); //GET SIZE

View File

@ -117,6 +117,29 @@ void why2_list_remove(why2_list_t *list, why2_node_t *node)
why2_deallocate(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) void why2_list_remove_back(why2_list_t *list)
{ {
why2_node_t *head = list -> head; why2_node_t *head = list -> head;