From: Lukáš Jiřiště Date: Sun, 5 Apr 2026 07:09:30 +0000 (+0200) Subject: Add head operations for linked list X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=65263632f86e465780a181266ee8cf7a389cc388;p=Libft.git Add head operations for linked list --- diff --git a/Makefile b/Makefile index ae7be26..8308ace 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ SRCstruct:= ft_stack_free.c \ ft_dict_insert.c \ ft_dict_traversal.c \ ft_llist.c \ - ft_llist_helpers.c \ + ft_llist_delete.c \ ft_graph.c \ ft_graph_helpers.c \ diff --git a/ft_struct/ft_llist.c b/ft_struct/ft_llist.c index 9303109..b41f5e4 100644 --- a/ft_struct/ft_llist.c +++ b/ft_struct/ft_llist.c @@ -35,31 +35,24 @@ t_llist_node *ft_llist_insert(t_llist *list, void *element) return (new_node); } -void *ft_llist_access(t_llist_node *node) +t_llist_node *ft_llist_insert_head(t_llist *list, void *element) { - return (&node->data); + t_llist_node *new_node; + + if (!list || !element) + return (NULL); + new_node = malloc(sizeof(*new_node) + list->el_size); + if (!new_node) + return (NULL); + ft_memcpy(&new_node->data, element, list->el_size); + new_node->next = list->head; + list->head = new_node; + return (new_node); } -void ft_llist_delete(t_llist *list, - t_llist_node *node, t_free_fun free_el) +void *ft_llist_access(t_llist_node *node) { - t_llist_node *parent; - - if (!list || !list->head || !node) - return ; - if (node == list->head) - { - list->head = node->next; - if (free_el) - free_el(ft_llist_access(node)); - free(node); - return ; - } - parent = list->head; - while (parent->next && parent->next != node) - parent = parent->next; - ft_llist_delete_next(parent, free_el); - return ; + return (&node->data); } void ft_llist_free(t_llist *list, t_free_fun free_el) diff --git a/ft_struct/ft_llist_delete.c b/ft_struct/ft_llist_delete.c new file mode 100644 index 0000000..b97bb27 --- /dev/null +++ b/ft_struct/ft_llist_delete.c @@ -0,0 +1,52 @@ +#include "ft_struct.h" +#include + +void ft_llist_delete_next(t_llist_node *node, t_free_fun free_el) +{ + t_llist_node *child; + + if (!node || !node->next) + return ; + child = node->next->next; + if (free_el) + free_el(ft_llist_access(node->next)); + free(node->next); + node->next = child; + return ; +} + +void ft_llist_delete(t_llist *list, + t_llist_node *node, t_free_fun free_el) +{ + t_llist_node *parent; + + if (!list || !list->head || !node) + return ; + if (node == list->head) + { + list->head = node->next; + if (free_el) + free_el(ft_llist_access(node)); + free(node); + return ; + } + parent = list->head; + while (parent->next && parent->next != node) + parent = parent->next; + ft_llist_delete_next(parent, free_el); + return ; +} + +void ft_llist_delete_head(t_llist *list, t_free_fun free_el) +{ + t_llist_node *deleted_node; + + if (!list || !list->head) + return ; + deleted_node = list->head; + list->head = deleted_node->next; + if (free_el) + free_el(ft_llist_access(deleted_node)); + free(deleted_node); + return ; +} diff --git a/ft_struct/ft_llist_helpers.c b/ft_struct/ft_llist_helpers.c deleted file mode 100644 index b731bcf..0000000 --- a/ft_struct/ft_llist_helpers.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "ft_struct.h" -#include - -void ft_llist_delete_next(t_llist_node *node, t_free_fun free_el) -{ - t_llist_node *child; - - if (!node || !node->next) - return ; - child = node->next->next; - if (free_el) - free_el(ft_llist_access(node->next)); - free(node->next); - node->next = child; - return ; -} - diff --git a/inc/ft_struct.h b/inc/ft_struct.h index 6e500f0..b0f3fba 100644 --- a/inc/ft_struct.h +++ b/inc/ft_struct.h @@ -167,11 +167,13 @@ t_ft_stat ft_llist_init(t_llist *list, size_t el_size); // the inserted node instead of t_ft_stat (so the user needs not traverse). // NULL signifies an error (probably failed allocation of memory). t_llist_node *ft_llist_insert(t_llist *list, void *element); +t_llist_node *ft_llist_insert_head(t_llist *list, void *element); void *ft_llist_access(t_llist_node *node); void ft_llist_delete(t_llist *list, t_llist_node *node, t_free_fun free_el); void ft_llist_delete_next( t_llist_node *node, t_free_fun free_el); +void ft_llist_delete_head(t_llist *list, t_free_fun free_el); void ft_llist_free(t_llist *list, t_free_fun free_el); typedef struct s_graph_node