From 21a7ed87d56d5cc3af7aba8bad4189953e2f62eb Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Wed, 28 Jun 2023 18:03:48 +0200 Subject: [PATCH 1/1] Solution to everything in C 12. Everything compiles to object file and everything passes norminette. Nothing was tested by running a program. --- ex00/ft_create_elem.c | 27 +++++++++++++ ex00/ft_list.h | 22 +++++++++++ ex01/ft_list.h | 22 +++++++++++ ex01/ft_list_push_front.c | 37 ++++++++++++++++++ ex02/ft_list.h | 22 +++++++++++ ex02/ft_list_size.c | 26 +++++++++++++ ex03/ft_list.h | 22 +++++++++++ ex03/ft_list_last.c | 22 +++++++++++ ex04/ft_list.h | 22 +++++++++++ ex04/ft_list_push_back.c | 45 ++++++++++++++++++++++ ex05/ft_list.h | 22 +++++++++++ ex05/ft_list_push_strs.c | 67 ++++++++++++++++++++++++++++++++ ex06/ft_list.h | 22 +++++++++++ ex07/ft_list.h | 22 +++++++++++ ex07/ft_list_at.c | 28 ++++++++++++++ ex07/ft_list_clear.c | 30 +++++++++++++++ ex08/ft_list.h | 22 +++++++++++ ex08/ft_list_reverse.c | 33 ++++++++++++++++ ex09/ft_list.h | 22 +++++++++++ ex09/ft_list_foreach.c | 23 +++++++++++ ex10/ft_list.h | 22 +++++++++++ ex10/ft_list_foreach_if.c | 25 ++++++++++++ ex11/ft_list.h | 22 +++++++++++ ex11/ft_list_find.c | 25 ++++++++++++ ex12/ft_list.h | 22 +++++++++++ ex12/ft_list_remove_if.c | 39 +++++++++++++++++++ ex13/ft_list.h | 22 +++++++++++ ex13/ft_list_merge.c | 36 +++++++++++++++++ ex14/ft_list.h | 22 +++++++++++ ex14/ft_list_sort.c | 65 +++++++++++++++++++++++++++++++ ex15/ft_list.h | 22 +++++++++++ ex15/ft_list_reverse_fun.c | 66 +++++++++++++++++++++++++++++++ ex16/ft_list.h | 22 +++++++++++ ex16/ft_sorted_list_insert.c | 75 ++++++++++++++++++++++++++++++++++++ ex17/ft_list.h | 22 +++++++++++ ex17/ft_sorted_list_merge.c | 46 ++++++++++++++++++++++ 36 files changed, 1111 insertions(+) create mode 100644 ex00/ft_create_elem.c create mode 100644 ex00/ft_list.h create mode 100644 ex01/ft_list.h create mode 100644 ex01/ft_list_push_front.c create mode 100644 ex02/ft_list.h create mode 100644 ex02/ft_list_size.c create mode 100644 ex03/ft_list.h create mode 100644 ex03/ft_list_last.c create mode 100644 ex04/ft_list.h create mode 100644 ex04/ft_list_push_back.c create mode 100644 ex05/ft_list.h create mode 100644 ex05/ft_list_push_strs.c create mode 100644 ex06/ft_list.h create mode 100644 ex07/ft_list.h create mode 100644 ex07/ft_list_at.c create mode 100644 ex07/ft_list_clear.c create mode 100644 ex08/ft_list.h create mode 100644 ex08/ft_list_reverse.c create mode 100644 ex09/ft_list.h create mode 100644 ex09/ft_list_foreach.c create mode 100644 ex10/ft_list.h create mode 100644 ex10/ft_list_foreach_if.c create mode 100644 ex11/ft_list.h create mode 100644 ex11/ft_list_find.c create mode 100644 ex12/ft_list.h create mode 100644 ex12/ft_list_remove_if.c create mode 100644 ex13/ft_list.h create mode 100644 ex13/ft_list_merge.c create mode 100644 ex14/ft_list.h create mode 100644 ex14/ft_list_sort.c create mode 100644 ex15/ft_list.h create mode 100644 ex15/ft_list_reverse_fun.c create mode 100644 ex16/ft_list.h create mode 100644 ex16/ft_sorted_list_insert.c create mode 100644 ex17/ft_list.h create mode 100644 ex17/ft_sorted_list_merge.c diff --git a/ex00/ft_create_elem.c b/ex00/ft_create_elem.c new file mode 100644 index 0000000..a0897ca --- /dev/null +++ b/ex00/ft_create_elem.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_create_elem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 11:24:10 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 14:13:45 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +t_list *ft_create_elem(void *data) +{ + t_list *new_elem; + + new_elem = malloc(sizeof(t_list)); + if (new_elem) + { + new_elem->data = data; + new_elem->next = NULL; + } + return (new_elem); +} diff --git a/ex00/ft_list.h b/ex00/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex00/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex01/ft_list.h b/ex01/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex01/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex01/ft_list_push_front.c b/ex01/ft_list_push_front.c new file mode 100644 index 0000000..c02ec72 --- /dev/null +++ b/ex01/ft_list_push_front.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_push_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 11:27:15 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 14:16:03 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +t_list *ft_create_elem(void *data) +{ + t_list *new_elem; + + new_elem = malloc(sizeof(t_list)); + if (new_elem) + { + new_elem->data = data; + new_elem->next = NULL; + } + return (new_elem); +} + +void ft_list_push_front(t_list **begin_list, void *data) +{ + t_list *new_elem; + + new_elem = ft_create_elem(data); + new_elem->next = *begin_list; + *begin_list = new_elem; + return ; +} diff --git a/ex02/ft_list.h b/ex02/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex02/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex02/ft_list_size.c b/ex02/ft_list_size.c new file mode 100644 index 0000000..cd84d5a --- /dev/null +++ b/ex02/ft_list_size.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 12:26:21 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 12:28:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +int ft_list_size(t_list *begin_list) +{ + int i; + + i = 0; + while (begin_list) + { + ++i; + begin_list = begin_list->next; + } + return (i); +} diff --git a/ex03/ft_list.h b/ex03/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex03/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex03/ft_list_last.c b/ex03/ft_list_last.c new file mode 100644 index 0000000..3800fc2 --- /dev/null +++ b/ex03/ft_list_last.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_last.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 12:29:08 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 12:35:08 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_list_last(t_list *begin_list) +{ + if (!begin_list) + return (begin_list); + while (begin_list->next) + begin_list = begin_list->next; + return (begin_list); +} diff --git a/ex04/ft_list.h b/ex04/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex04/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex04/ft_list_push_back.c b/ex04/ft_list_push_back.c new file mode 100644 index 0000000..1a91695 --- /dev/null +++ b/ex04/ft_list_push_back.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_push_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 12:40:16 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 14:14:46 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +t_list *ft_list_last(t_list *begin_list) +{ + if (!begin_list) + return (begin_list); + while (begin_list->next) + begin_list = begin_list->next; + return (begin_list); +} + +t_list *ft_create_elem(void *data) +{ + t_list *new_elem; + + new_elem = malloc(sizeof(t_list)); + if (new_elem) + { + new_elem->data = data; + new_elem->next = NULL; + } + return (new_elem); +} + +void ft_list_push_back(t_list **begin_list, void *data) +{ + t_list *last_elem; + + last_elem = ft_list_last(*begin_list); + last_elem->next = ft_create_elem(data); + return ; +} diff --git a/ex05/ft_list.h b/ex05/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex05/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex05/ft_list_push_strs.c b/ex05/ft_list_push_strs.c new file mode 100644 index 0000000..bf9239a --- /dev/null +++ b/ex05/ft_list_push_strs.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_push_strs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 12:45:53 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:48:20 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +t_list *ft_create_elem(void *data) +{ + t_list *new_elem; + + new_elem = malloc(sizeof(t_list)); + if (new_elem) + { + new_elem->data = data; + new_elem->next = NULL; + } + return (new_elem); +} + +void ft_list_clear(t_list *begin_list, void (*free_fct)(void*)) +{ + t_list *cur; + t_list *next; + + cur = begin_list; + while (cur) + { + free_fct(cur->data); + next = cur->next; + free(cur); + cur = next; + } + return ; +} + +t_list *ft_list_push_strs(int size, char **strs) +{ + t_list *beginning; + t_list *current_el; + int i; + + beginning = NULL; + i = 0; + if (size > 0) + beginning = ft_create_elem(strs[i++]); + current_el = beginning; + while (i < size) + { + current_el->next = ft_create_elem(strs[i++]); + if (!current_el->next) + { + ft_list_clear(beginning, &free); + return (NULL); + } + current_el = current_el->next; + } + return (beginning); +} diff --git a/ex06/ft_list.h b/ex06/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex06/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex07/ft_list.h b/ex07/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex07/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex07/ft_list_at.c b/ex07/ft_list_at.c new file mode 100644 index 0000000..4516675 --- /dev/null +++ b/ex07/ft_list_at.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_at.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:19:21 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 14:37:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_list_at(t_list *begin_list, unsigned int nbr) +{ + unsigned int i; + t_list *el; + + i = 0; + el = begin_list; + while (i < nbr && el) + { + el = el->next; + ++i; + } + return (el); +} diff --git a/ex07/ft_list_clear.c b/ex07/ft_list_clear.c new file mode 100644 index 0000000..8b0a0eb --- /dev/null +++ b/ex07/ft_list_clear.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:17:41 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 14:19:43 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +void ft_list_clear(t_list *begin_list, void (*free_fct)(void*)) +{ + t_list *cur; + t_list *next; + + cur = begin_list; + while (cur) + { + free_fct(cur->data); + next = cur->next; + free(cur); + cur = next; + } + return ; +} diff --git a/ex08/ft_list.h b/ex08/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex08/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex08/ft_list_reverse.c b/ex08/ft_list_reverse.c new file mode 100644 index 0000000..25b6816 --- /dev/null +++ b/ex08/ft_list_reverse.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:22:47 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:39:39 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +void ft_list_reverse(t_list **begin_list) +{ + t_list *prev; + t_list *current; + t_list *next; + + prev = NULL; + current = *begin_list; + while (current) + { + next = current->next; + current->next = prev; + prev = current; + current = next; + } + *begin_list = prev; + return ; +} diff --git a/ex09/ft_list.h b/ex09/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex09/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex09/ft_list_foreach.c b/ex09/ft_list_foreach.c new file mode 100644 index 0000000..d8940b7 --- /dev/null +++ b/ex09/ft_list_foreach.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_foreach.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:38:44 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 14:46:49 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +void ft_list_foreach(t_list *begin_list, void (*f)(void*)) +{ + while (begin_list) + { + f(begin_list->data); + begin_list = begin_list->next; + } + return ; +} diff --git a/ex10/ft_list.h b/ex10/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex10/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex10/ft_list_foreach_if.c b/ex10/ft_list_foreach_if.c new file mode 100644 index 0000000..491f6fe --- /dev/null +++ b/ex10/ft_list_foreach_if.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_foreach_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:42:33 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 18:02:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +void ft_list_foreach_if(t_list *begin_list, void (*f)(void*), + void *data_ref, int (*cmp)()) +{ + while (begin_list) + { + if (cmp(begin_list->data, data_ref) == 0) + f(begin_list->data); + begin_list = begin_list->next; + } + return ; +} diff --git a/ex11/ft_list.h b/ex11/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex11/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex11/ft_list_find.c b/ex11/ft_list_find.c new file mode 100644 index 0000000..16decd8 --- /dev/null +++ b/ex11/ft_list_find.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:47:42 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:44:22 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +t_list *ft_list_find(t_list *begin_list, void *data_ref, int (*cmp)()) +{ + while (begin_list) + { + if (cmp(begin_list->data, data_ref) == 0) + return (begin_list); + begin_list = begin_list->next; + } + return (NULL); +} diff --git a/ex12/ft_list.h b/ex12/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex12/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex12/ft_list_remove_if.c b/ex12/ft_list_remove_if.c new file mode 100644 index 0000000..971889b --- /dev/null +++ b/ex12/ft_list_remove_if.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_remove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:49:53 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:51:00 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +void remove_next_el(t_list *prev, t_list **cur, void (*free_fct)(void*)) +{ + prev->next = (*cur)->next; + free_fct((*cur)->data); + free(*cur); + *cur = prev->next; + return ; +} + +void ft_list_remove_if(t_list **begin_list, void *data_ref, + int (*cmp)(), void (*free_fct)(void*)) +{ + t_list *prev; + + prev = NULL; + while (*begin_list) + { + if (cmp((*begin_list)->data, data_ref) == 0) + remove_next_el(prev, begin_list, free_fct); + else + *begin_list = (*begin_list)->next; + } + return ; +} diff --git a/ex13/ft_list.h b/ex13/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex13/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex13/ft_list_merge.c b/ex13/ft_list_merge.c new file mode 100644 index 0000000..135a59b --- /dev/null +++ b/ex13/ft_list_merge.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:09:00 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:40:47 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_list_last(t_list *begin_list) +{ + if (!begin_list) + return (begin_list); + while (begin_list->next) + begin_list = begin_list->next; + return (begin_list); +} + +void ft_list_merge(t_list **begin_list1, t_list *begin_list2) +{ + t_list *last; + + if (!*begin_list1) + { + *begin_list1 = begin_list2; + return ; + } + last = ft_list_last(*begin_list1); + last->next = begin_list2; + return ; +} diff --git a/ex14/ft_list.h b/ex14/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex14/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex14/ft_list_sort.c b/ex14/ft_list_sort.c new file mode 100644 index 0000000..2a42722 --- /dev/null +++ b/ex14/ft_list_sort.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:13:29 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:48:42 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +int ft_list_size(t_list *begin_list) +{ + int i; + + i = 0; + while (begin_list) + { + ++i; + begin_list = begin_list->next; + } + return (i); +} + +void ft_swap(void **data1, void **data2) +{ + void *temp; + + temp = *data1; + *data1 = *data2; + *data2 = temp; + return ; +} + +void ft_max_to_end(t_list *begin_list, int (*cmp)(), int size) +{ + int i; + + i = 0; + while (i < size - 1) + { + if (cmp(begin_list->data, begin_list->next->data) > 0) + ft_swap(&(begin_list->data), &(begin_list->next->data)); + begin_list = begin_list->next; + } + return ; +} + +void ft_list_sort(t_list **begin_list, int (*cmp)()) +{ + int size; + int i; + + size = ft_list_size(*begin_list); + i = 0; + while (i < size) + { + ft_max_to_end(*begin_list, cmp, size - i); + ++i; + } + return ; +} diff --git a/ex15/ft_list.h b/ex15/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex15/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex15/ft_list_reverse_fun.c b/ex15/ft_list_reverse_fun.c new file mode 100644 index 0000000..b671e2a --- /dev/null +++ b/ex15/ft_list_reverse_fun.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_reverse_fun.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:45:46 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:41:28 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +int ft_list_size(t_list *begin_list) +{ + int i; + + i = 0; + while (begin_list) + { + ++i; + begin_list = begin_list->next; + } + return (i); +} + +void ft_swap(void **data1, void **data2) +{ + void *temp; + + temp = *data1; + *data1 = *data2; + *data2 = temp; + return ; +} + +void ft_first_to_end(t_list *begin_list, int size) +{ + int i; + + i = 0; + while (i < size - 1) + { + ft_swap(&(begin_list->data), &(begin_list->next->data)); + begin_list = begin_list->next; + } + return ; +} + +// This algorithm could be vastly faster, +// this implementation performs n^2/2 swaps but only n/2 is necessary +void ft_list_reverse_fun(t_list *begin_list) +{ + int i; + int size; + + size = ft_list_size(begin_list); + i = 0; + while (i < size) + { + ft_first_to_end(begin_list, size - i); + ++i; + } + return ; +} diff --git a/ex16/ft_list.h b/ex16/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex16/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex16/ft_sorted_list_insert.c b/ex16/ft_sorted_list_insert.c new file mode 100644 index 0000000..6d4e38e --- /dev/null +++ b/ex16/ft_sorted_list_insert.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sorted_list_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:53:26 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:51:43 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +t_list *ft_create_elem(void *data) +{ + t_list *new_elem; + + new_elem = malloc(sizeof(t_list)); + if (new_elem) + { + new_elem->data = data; + new_elem->next = NULL; + } + return (new_elem); +} + +void insert_after(t_list *prev, void *data) +{ + t_list *new; + + new = ft_create_elem(data); + if (!new) + return ; + new->next = prev->next; + prev->next = new->next; + return ; +} + +void ft_list_push_front(t_list **begin_list, void *data) +{ + t_list *new_elem; + + new_elem = ft_create_elem(data); + new_elem->next = *begin_list; + *begin_list = new_elem; + return ; +} + +void ft_sorted_list_insert(t_list **begin_list, void *data, int (*cmp)()) +{ + t_list *prev; + t_list *cur; + + if (!*begin_list) + { + *begin_list = ft_create_elem(data); + return ; + } + prev = NULL; + cur = *begin_list; + while (cur) + { + if (cmp(data, cur->data) <= 0) + break ; + prev = cur; + cur = cur->next; + } + if (prev) + insert_after(prev, data); + else + ft_list_push_front(begin_list, data); + return ; +} diff --git a/ex17/ft_list.h b/ex17/ft_list.h new file mode 100644 index 0000000..40ed34e --- /dev/null +++ b/ex17/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 10:27:34 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 13:56:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/ex17/ft_sorted_list_merge.c b/ex17/ft_sorted_list_merge.c new file mode 100644 index 0000000..705239c --- /dev/null +++ b/ex17/ft_sorted_list_merge.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sorted_list_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 17:13:15 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 17:50:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +void exchange_list(t_list *last_staying, t_list **first_other) +{ + t_list *new_other; + + new_other = last_staying->next; + last_staying->next = *first_other; + *first_other = new_other; + return ; +} + +void ft_sorted_list_merge(t_list **begin_list1, t_list *other_list, + int (*cmp)()) +{ + t_list *main; + + if (cmp((*begin_list1)->data, other_list->data) > 0) + { + main = other_list; + other_list = *begin_list1; + *begin_list1 = main; + } + else + main = *begin_list1; + while (main->next) + { + if (cmp(main->next->data, other_list->data) > 0) + exchange_list(main, &other_list); + main = main->next; + } + main->next = other_list; + return ; +} -- 2.30.2