created why2_list_reverse fn in llist

it reverses list you dipstick
This commit is contained in:
Václav Šmejkal 2024-11-21 17:59:15 +01:00
parent 90643d8247
commit 7dcbd4fc80
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
2 changed files with 34 additions and 1 deletions

View File

@ -46,6 +46,7 @@ void why2_list_remove_at(why2_list_t *list, unsigned long index); //REMOVE ELEME
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
void why2_list_reverse(why2_list_t *list, unsigned long size); //REVERSES list
#ifdef __cplusplus
}

View File

@ -119,7 +119,7 @@ void why2_list_remove(why2_list_t *list, why2_node_t *node)
void why2_list_remove_at(why2_list_t *list, unsigned long index)
{
if (list == NULL) return; //EMPTY LIST
if (list -> head == NULL) return; //EMPTY LIST
why2_node_t *node_to_remove;
@ -202,3 +202,35 @@ unsigned long why2_list_get_size(why2_list_t *list)
return n;
}
void why2_list_reverse(why2_list_t *list, unsigned long size)
{
if (list -> head == NULL) return; //LIST IS EMPTY
why2_list_t reversed_list = WHY2_LIST_EMPTY;
why2_node_t *buffer = list -> head;
why2_node_t *buffer2;
//REVERSE INTO reversed_list AND DEALLOCATE list
do
{
//COPY
why2_node_t *current_node = why2_malloc(sizeof(why2_node_t));
current_node -> value = why2_malloc(size);
memcpy(current_node -> value, buffer -> value, size);
//INSERT INTO reversed_list
current_node -> next = reversed_list.head; //CHANGE NEXT POINTER
reversed_list.head = current_node; //INSERT
buffer2 = buffer;
buffer = buffer -> next; //ITER
//DEALLOCATE
why2_deallocate(buffer2 -> value);
why2_deallocate(buffer2);
} while (buffer != NULL);
//SET list TO reversed_list
list -> head = reversed_list.head;
}