]> www.ljiriste.work Git - Libft.git/commitdiff
Add head operations for linked list
authorLukáš Jiřiště <redacted>
Sun, 5 Apr 2026 07:09:30 +0000 (09:09 +0200)
committerLukáš Jiřiště <redacted>
Sun, 5 Apr 2026 07:09:30 +0000 (09:09 +0200)
Makefile
ft_struct/ft_llist.c
ft_struct/ft_llist_delete.c [new file with mode: 0644]
ft_struct/ft_llist_helpers.c [deleted file]
inc/ft_struct.h

index ae7be265a8ed8e808ba14afc42fbd5a990074438..8308ace7cc76dbcc6e0e5d070047705cb28af794 100644 (file)
--- 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                                                                              \
 
index 9303109d499cab8e47d0bb9a94b0fea4c5b7590e..b41f5e44b5f66616ac09b45d3f64267f607fffbc 100644 (file)
@@ -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 (file)
index 0000000..b97bb27
--- /dev/null
@@ -0,0 +1,52 @@
+#include "ft_struct.h"
+#include <stdlib.h>
+
+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 (file)
index b731bcf..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-#include "ft_struct.h"
-#include <stdlib.h>
-
-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 ;
-}
-
index 6e500f06e1b0ee68916fbbc8d152549dc38db026..b0f3fbaa991c9fb773ed3585703cd50db74ad30f 100644 (file)
@@ -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