]> www.ljiriste.work Git - Libft.git/commitdiff
Rewrite the stack to use linked list
authorLukáš Jiřiště <redacted>
Mon, 6 Apr 2026 10:18:01 +0000 (12:18 +0200)
committerLukáš Jiřiště <redacted>
Mon, 6 Apr 2026 10:18:01 +0000 (12:18 +0200)
ft_struct/ft_stack_free.c
ft_struct/ft_stack_init.c
ft_struct/ft_stack_pop.c
ft_struct/ft_stack_pop_forget.c
ft_struct/ft_stack_push.c
ft_struct/ft_stack_top.c
inc/ft_struct.h

index 66148f5ee20f2ed686b8060cbb5f915d84232369..d057d6b640c6f76eab6b79f7b525f6d657e03bf8 100644 (file)
@@ -15,6 +15,6 @@
 
 void   ft_stack_free(t_stack *stack, void (*free_el)(void *))
 {
-       ft_vec_free(&stack->vec, free_el);
+       ft_llist_free(&stack->list, free_el);
        return ;
 }
index 6e8e0d540553996ceee5b4422197d37abdc372bc..3f7feaccd980b11529b2b3f908f35ae8b2c077de 100644 (file)
@@ -16,5 +16,5 @@
 
 t_ft_stat      ft_stack_init(t_stack *stack, size_t el_size)
 {
-       return (ft_vec_init(&stack->vec, el_size));
+       return (ft_llist_init(&stack->list, el_size));
 }
index 1fd7db68b1565c2e0c206f370ad0a3110be062c5..0f2ce9fff72de9a6c32beca592df7691d229b02a 100644 (file)
@@ -18,7 +18,6 @@ void  *ft_stack_pop(t_stack *stack, void (*free_el)(void *))
        void    *res;
 
        res = ft_stack_top(stack);
-       if (res)
-               ft_vec_erase(&stack->vec, stack->vec.size - 1, free_el);
+       ft_llist_delete_head(&stack->list, free_el);
        return (res);
 }
index b0b0095bf24dca28a5583d00d224d7576e94ad6a..3d51c679270df12eb3badcfabdd335c6d393addf 100644 (file)
 
 void   *ft_stack_pop_forget(t_stack *stack)
 {
-       void    *res;
-
-       res = ft_stack_top(stack);
-       if (res)
-               ft_vec_forget(&stack->vec, stack->vec.size - 1);
-       return (res);
+       return (ft_stack_pop(stack, NULL));
 }
index 9ad3319b0f20d93059c610f8bacb7724101786d6..db72c04cdeec066a0c119259be35774533c18a6d 100644 (file)
 
 t_ft_stat      ft_stack_push(t_stack *stack, void *element)
 {
-       return (ft_vec_append(&stack->vec, element));
+       t_llist_node    *new_node;
+
+       if (!stack || !element)
+       {
+               return (invalid_input);
+       }
+       new_node = ft_llist_insert_head(&stack->list, element);
+       if (!new_node)
+       {
+               return (alloc_fail);
+       }
+       return (success);
 }
index 36dd701b923663a139604c617be9c58a210f276c..be7a55a1724026413298dd444a663358c65477e6 100644 (file)
@@ -16,7 +16,7 @@
 
 void   *ft_stack_top(t_stack *stack)
 {
-       if (stack->vec.size == 0)
+       if (!stack->list.head)
                return (NULL);
-       return (ft_vec_access(&stack->vec, stack->vec.size - 1));
+       return (ft_llist_access(stack->list.head));
 }
index b0f3fbaa991c9fb773ed3585703cd50db74ad30f..ff2dccf37d90bda1823a11af3dce725d4c7a2906 100644 (file)
@@ -20,9 +20,37 @@ extern "C" {
 # include "ft_arr.h"
 # include <sys/types.h>
 
+typedef struct s_linked_list_node t_llist_node;
+
+struct s_linked_list_node
+{
+       t_llist_node    *next;
+       char                    data[];
+};
+
+typedef struct s_linked_list
+{
+       size_t                  el_size;
+       t_llist_node    *head;
+}                                      t_llist;
+
+t_ft_stat                      ft_llist_init(t_llist *list, size_t el_size);
+// Because of notoriously slow access, the insert function returns a pointer to
+// 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_stack
 {
-       t_vec   vec;
+       t_llist list;
 }                      t_stack;
 
 t_ft_stat      ft_stack_init(t_stack *stack, size_t el_size);
@@ -148,34 +176,6 @@ t_dict_entry       ft_dict_traverse_full(t_dict_traversal *traversal);
 void                   *ft_dict_traverse(t_dict_traversal *traversal);
 void                   ft_dict_traversal_free(t_dict_traversal *traversal);
 
-typedef struct s_linked_list_node t_llist_node;
-
-struct s_linked_list_node
-{
-       t_llist_node    *next;
-       char                    data[];
-};
-
-typedef struct s_linked_list
-{
-       size_t                  el_size;
-       t_llist_node    *head;
-}                                      t_llist;
-
-t_ft_stat                      ft_llist_init(t_llist *list, size_t el_size);
-// Because of notoriously slow access, the insert function returns a pointer to
-// 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
 {
        t_vec   connected_nodes;