Added bonus functions and changed libft.h and Makefile accordingly.
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 16 Aug 2023 15:46:11 +0000 (17:46 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 16 Aug 2023 15:46:11 +0000 (17:46 +0200)
Makefile
ft_lstadd_back.c [new file with mode: 0644]
ft_lstadd_front.c [new file with mode: 0644]
ft_lstclear.c [new file with mode: 0644]
ft_lstdelone.c [new file with mode: 0644]
ft_lstiter.c [new file with mode: 0644]
ft_lstlast.c [new file with mode: 0644]
ft_lstmap.c [new file with mode: 0644]
ft_lstnew.c [new file with mode: 0644]
ft_lstsize.c [new file with mode: 0644]
libft.h

index 1ae2e12ae3512f43b37ac8d5366050e844942833..3f514456670365db167c9cc0d3656bd7e433746d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,11 @@ INCDIR = .
 SRCDIR = .
 # SOURCES = $(addprefix $(SRCDIR)/, ft_putchar.c ft_putstr.c ft_strcmp.c ft_strlen.c ft_swap.c)
 SOURCES = $(shell find $(SRCDIR) -name "*.c")
+BONUS = $(shell find $(SRCDIR) -name "*lst*.c")
+TEMP := $(SOURCES)
+SOURCES = $(filter-out $(BONUS), $(TEMP))
 OBJECTS = $(SOURCES:.c=.o)
+BONUS_OBJECTS = $(BONUS:.c=.o)
 
 NAME = libft.a
 
@@ -21,10 +25,14 @@ $(NAME) : $(OBJECTS)
        $(CC) $(CFLAGS) -o $@ -c $< -I $(INCDIR)
 
 clean :
-       $(RM) $(OBJECTS)
+       $(RM) $(OBJECTS) $(BONUS_OBJECTS)
 
 fclean : clean 
        $(RM) $(NAME)
 
 re : fclean
        $(MAKE) all
+
+#bonus : CFLAGS += -DBONUS
+bonus : $(OBJECTS) $(BONUS_OBJECTS)
+       $(AR) rcs $(NAME) $(OBJECTS) $(BONUS_OBJECTS)
diff --git a/ft_lstadd_back.c b/ft_lstadd_back.c
new file mode 100644 (file)
index 0000000..be63a9e
--- /dev/null
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstadd_back.c                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:33:28 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 16:52:43 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include "libft.h"
+
+void   ft_lstadd_back(t_list **lst, t_list *new)
+{
+       if (*lst == NULL)
+       {
+               *lst = new;
+               return ;
+       }
+       ft_lstlast(*lst)->next = new;
+       return ;
+}
diff --git a/ft_lstadd_front.c b/ft_lstadd_front.c
new file mode 100644 (file)
index 0000000..4995275
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstadd_front.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:26:20 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 13:42:49 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_lstadd_front(t_list **lst, t_list *new)
+{
+       new->next = *lst;
+       *lst = new;
+       return ;
+}
diff --git a/ft_lstclear.c b/ft_lstclear.c
new file mode 100644 (file)
index 0000000..afd9211
--- /dev/null
@@ -0,0 +1,27 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstclear.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:45:28 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 16:48:58 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+void   ft_lstclear(t_list **lst, void (*del)(void *))
+{
+       t_list  *temp;
+
+       while (*lst)
+       {
+               temp = (*lst)->next;
+               ft_lstdelone(*lst, del);
+               *lst = temp;
+       }
+       return ;
+}
diff --git a/ft_lstdelone.c b/ft_lstdelone.c
new file mode 100644 (file)
index 0000000..622b3eb
--- /dev/null
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstdelone.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:43:02 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 13:46:37 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+void   ft_lstdelone(t_list *lst, void (*del)(void *))
+{
+       del(lst->content);
+       free(lst);
+       return ;
+}
diff --git a/ft_lstiter.c b/ft_lstiter.c
new file mode 100644 (file)
index 0000000..c188fc0
--- /dev/null
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstiter.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:50:31 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 13:51:48 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_lstiter(t_list *lst, void (*f)(void *))
+{
+       while (lst)
+       {
+               f(lst->content);
+               lst = lst->next;
+       }
+       return ;
+}
diff --git a/ft_lstlast.c b/ft_lstlast.c
new file mode 100644 (file)
index 0000000..c544009
--- /dev/null
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstlast.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:31:01 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 17:14:03 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include "libft.h"
+
+t_list *ft_lstlast(t_list *lst)
+{
+       if (lst == NULL)
+               return (lst);
+       while (lst->next)
+               lst = lst->next;
+       return (lst);
+}
diff --git a/ft_lstmap.c b/ft_lstmap.c
new file mode 100644 (file)
index 0000000..d6810cc
--- /dev/null
@@ -0,0 +1,54 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstmap.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:51:57 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 16:58:41 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+static t_list  *first_el(t_list *lst, void *(*f)(void *), void (*del)(void *))
+{
+       t_list  *res;
+       void    *cont;
+
+       if (lst == NULL)
+               return (NULL);
+       cont = f(lst->content);
+       res = ft_lstnew(cont);
+       if (res == NULL)
+               del(cont);
+       return (res);
+}
+
+t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
+{
+       t_list  *res;
+       t_list  *new_node;
+       void    *new_cont;
+
+       res = first_el(lst, f, del);
+       if (res == NULL)
+               return (res);
+       new_node = res;
+       lst = lst->next;
+       while (lst && res)
+       {
+               new_cont = f(lst->content);
+               new_node->next = ft_lstnew(new_cont);
+               new_node = new_node->next;
+               if (new_node == NULL)
+               {
+                       del(new_cont);
+                       ft_lstclear(&res, del);
+               }
+               lst = lst->next;
+       }
+       return (res);
+}
diff --git a/ft_lstnew.c b/ft_lstnew.c
new file mode 100644 (file)
index 0000000..912fdc7
--- /dev/null
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstnew.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:23:00 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 13:26:10 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+t_list *ft_lstnew(void *content)
+{
+       t_list  *res;
+
+       res = malloc(sizeof(t_list));
+       if (res == NULL)
+               return (res);
+       res->content = content;
+       res->next = NULL;
+       return (res);
+}
diff --git a/ft_lstsize.c b/ft_lstsize.c
new file mode 100644 (file)
index 0000000..cb2dc8f
--- /dev/null
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstsize.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/16 13:28:58 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/16 14:59:47 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include "libft.h"
+
+int    ft_lstsize(t_list *lst)
+{
+       int     res;
+
+       if (lst == NULL)
+               return (0);
+       res = 1;
+       while (lst->next)
+       {
+               lst = lst->next;
+               ++res;
+       }
+       return (res);
+}
diff --git a/libft.h b/libft.h
index 28f01be25143436bb1f2aca885cc98b31caf9ab8..1d8eb2581ae34249c6826649d4cd79267578651d 100644 (file)
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/08/15 12:58:15 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/08/15 16:42:58 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/08/16 17:12:57 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -61,4 +61,24 @@ void ft_putstr_fd(char *s, int fd);
 void   ft_putendl_fd(char *s, int fd);
 void   ft_putnbr_fd(int n, int fd);
 
+//# ifdef BONUS
+
+typedef struct s_list
+{
+       void                    *content;
+       struct s_list   *next;
+}                                      t_list;
+
+t_list *ft_lstnew(void *content);
+void   ft_lstadd_front(t_list **lst, t_list *new);
+int            ft_lstsize(t_list *lst);
+t_list *ft_lstlast(t_list *lst);
+void   ft_lstadd_back(t_list **lst, t_list *new);
+void   ft_lstdelone(t_list *lst, void (*del)(void *));
+void   ft_lstclear(t_list **lst, void (*del)(void *));
+void   ft_lstiter(t_list *lst, void (*f)(void *));
+t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
+
+//# endif
+
 #endif