From: Lukáš Jiřiště Date: Mon, 6 Apr 2026 10:56:31 +0000 (+0200) Subject: Optimize node insertion into linked list X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=8aca47d71653775ab6b0d3af27a3d5f1f7259218;p=Libft.git Optimize node insertion into linked list Linked list now holds a pointer to its last element so there is no need for iterating through the whole list for appending. Insertion into list was also moved to a new file. --- diff --git a/Makefile b/Makefile index 8308ace..1f8b8a2 100644 --- a/Makefile +++ b/Makefile @@ -48,6 +48,7 @@ SRCstruct:= ft_stack_free.c \ ft_dict_insert.c \ ft_dict_traversal.c \ ft_llist.c \ + ft_llist_insert.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 b41f5e4..b7e33b2 100644 --- a/ft_struct/ft_llist.c +++ b/ft_struct/ft_llist.c @@ -11,45 +11,6 @@ t_ft_stat ft_llist_init(t_llist *list, size_t el_size) return (success); } -t_llist_node *ft_llist_insert(t_llist *list, void *element) -{ - t_llist_node *node; - 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 = NULL; - node = list->head; - if (!node) - { - list->head = new_node; - return (new_node); - } - while (node->next) - node = node->next; - node->next = new_node; - return (new_node); -} - -t_llist_node *ft_llist_insert_head(t_llist *list, void *element) -{ - 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_access(t_llist_node *node) { return (&node->data); diff --git a/ft_struct/ft_llist_insert.c b/ft_struct/ft_llist_insert.c new file mode 100644 index 0000000..913d782 --- /dev/null +++ b/ft_struct/ft_llist_insert.c @@ -0,0 +1,42 @@ +#include "ft_struct.h" +#include "libft.h" +#include + +t_llist_node *ft_llist_insert(t_llist *list, void *element) +{ + 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 = NULL; + if (list->tail) + { + list->tail->next = new_node; + list->tail = new_node; + } + else + { + list->head = new_node; + list->tail = new_node; + } + return (new_node); +} + +t_llist_node *ft_llist_insert_head(t_llist *list, void *element) +{ + 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); +} diff --git a/inc/ft_struct.h b/inc/ft_struct.h index ff2dccf..3d5dcb5 100644 --- a/inc/ft_struct.h +++ b/inc/ft_struct.h @@ -32,6 +32,7 @@ typedef struct s_linked_list { size_t el_size; t_llist_node *head; + t_llist_node *tail; } t_llist; t_ft_stat ft_llist_init(t_llist *list, size_t el_size);