created why2_list_push_at fn in llist

pushes to specific position
This commit is contained in:
Václav Šmejkal 2024-11-21 16:49:27 +01:00
parent 08181e5f19
commit 0e246be627
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
2 changed files with 26 additions and 0 deletions

View File

@ -40,6 +40,7 @@ typedef struct
//FUNCTIONS
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_back(why2_list_t *list); //REMOVE LAST ELEMENT
why2_node_t *why2_list_find(why2_list_t *list, void *value); //FIND ELEMENT IN LIST

View File

@ -51,6 +51,31 @@ void why2_list_push(why2_list_t *list, void *value, unsigned long size)
list -> head = buffer;
}
void why2_list_push_at(why2_list_t *list, unsigned long index, void *value, unsigned long size)
{
//CREATE NODE
why2_node_t *head = list -> head;
why2_node_t *new_node = malloc(sizeof(why2_node_t));
new_node -> value = malloc(size);
//INSERT DATA
memcpy(new_node -> value, value, size);
if (index != 0 && head != NULL) //ISN'T FIRST
{
why2_node_t *node_before = head;
for (unsigned long j = 0; j < index - 1 && j < why2_list_get_size(list); j++) node_before = node_before -> next; //SCROLL TO THE POSITION
new_node -> next = node_before -> next; //SEW THE LIST BACK
node_before -> next = new_node;
} else //ADD BEFORE THE WHOLE LIST
{
new_node -> next = head;
list -> head = new_node;
}
}
void why2_list_remove(why2_list_t *list, why2_node_t *node)
{
if (node == NULL) return; //NULL NODE