From 880102ae9358db130ef67cc9a7177a1e1de76875 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 17 Jul 2019 19:30:52 +0200 Subject: c10 tail stuff, c11 start, c12/c13 files --- c12/:w | 32 ++++++++++++++++ c12/ex00/ft_create_elem.c | 5 ++- c12/ex01/ft_list_push_front.c | 11 +++--- c12/ex03/ft_list_last.c | 4 +- c12/ex04/ft_list_push_back.c | 21 +++++----- c12/ex05/ft_list_push_strs.c | 24 +++++++----- c12/ex06/ft_list_clear.c | 8 ++-- c12/ex07/ft_list_at.c | 10 +---- c12/ex08/ft_list_reverse.c | 17 ++++++--- c12/ex09/ft_list.h | 4 +- c12/ex09/ft_list_foreach.c | 0 c12/ex10/ft_list_foreach_if.c | 0 c12/ex11/ft_list_find.c | 0 c12/ex12/ft_list_remove_if.c | 0 c12/ex13/ft_list_merge.c | 0 c12/ex14/ft_list_sort.c | 0 c12/ex15/ft_list_reverse_fun.c | 0 c12/ex16/ft_sorted_list_insert.c | 0 c12/ex17/ft_sorted_list_merge.c | 0 c12/main.c | 82 ++++++++++++++++++++++++++++++++++++++++ 20 files changed, 169 insertions(+), 49 deletions(-) create mode 100644 c12/:w create mode 100644 c12/ex09/ft_list_foreach.c create mode 100644 c12/ex10/ft_list_foreach_if.c create mode 100644 c12/ex11/ft_list_find.c create mode 100644 c12/ex12/ft_list_remove_if.c create mode 100644 c12/ex13/ft_list_merge.c create mode 100644 c12/ex14/ft_list_sort.c create mode 100644 c12/ex15/ft_list_reverse_fun.c create mode 100644 c12/ex16/ft_sorted_list_insert.c create mode 100644 c12/ex17/ft_sorted_list_merge.c create mode 100644 c12/main.c (limited to 'c12') diff --git a/c12/:w b/c12/:w new file mode 100644 index 0000000..2b1fe54 --- /dev/null +++ b/c12/:w @@ -0,0 +1,32 @@ +#include +#include "ex00/ft_list.h" +#include "ex00/ft_create_elem.c" +#include "ex01/ft_list_push_front.c" +/*#include "ex02/ft_list_size.c"*/ +/*#include "ex03/ft_list_last.c"*/ +/*#include "ex04/ft_list_push_back.c"*/ +/*#include "ex05/ft_list_push_strs.c"*/ +/*#include "ex06/ft_list_clear.c"*/ +/*#include "ex07/ft_list_at.c"*/ +/*#include "ex08/ft_list_reverse.c"*/ +/*#include "ex09/ft_list_foreach.c"*/ +/*#include "ex10/ft_list_foreach_if.c"*/ +/*#include "ex11/ft_list_find.c"*/ +/*#include "ex12/ft_list_remove_if.c"*/ +/*#include "ex13/ft_list_merge.c"*/ +/*#include "ex14/ft_list_sort.c"*/ +/*#include "ex15/ft_list_reverse_fun.c"*/ +/*#include "ex16/ft_sorted_list_insert.c"*/ +/*#include "ft_sorted_list_merge.c"*/ + +int main() +{ + t_list *list = NULL; + int a = 4; + void *data = &a; + + list = ft_create_elem(data); + printf("%d\n", list->data); + + +} diff --git a/c12/ex00/ft_create_elem.c b/c12/ex00/ft_create_elem.c index 8b6996a..092833b 100644 --- a/c12/ex00/ft_create_elem.c +++ b/c12/ex00/ft_create_elem.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 17:19:22 by cacharle #+# #+# */ -/* Updated: 2019/07/09 17:19:24 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 18:34:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,8 @@ t_list *ft_create_elem(void *data) { t_list *list; - list = (t_list*)malloc(sizeof(t_list)); + if((list = (t_list*)malloc(sizeof(t_list))) == NULL) + return (NULL); list->data = data; list->next = NULL; return (list); diff --git a/c12/ex01/ft_list_push_front.c b/c12/ex01/ft_list_push_front.c index 788fce1..76fedd9 100644 --- a/c12/ex01/ft_list_push_front.c +++ b/c12/ex01/ft_list_push_front.c @@ -6,19 +6,18 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 17:19:14 by cacharle #+# #+# */ -/* Updated: 2019/07/09 17:19:15 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 17:10:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ + #include "ft_list.h" void ft_list_push_front(t_list **begin_list, void *data) { - t_list new_front; + t_list *new_front; - new_front = (t_list*)malloc(sizeof(t_list)); - new_front->data = data; + new_front = ft_create_elem(data); new_front->next = *begin_list; - if (*begin_list) - *begin_list = new_front; + *begin_list = new_front; } diff --git a/c12/ex03/ft_list_last.c b/c12/ex03/ft_list_last.c index 0446f76..84a46c2 100644 --- a/c12/ex03/ft_list_last.c +++ b/c12/ex03/ft_list_last.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 17:18:58 by cacharle #+# #+# */ -/* Updated: 2019/07/09 17:18:59 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 17:30:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_list *ft_list_last(t_list *begin_list) { - while (begin_list->next) + while (begin_list->next != NULL) begin_list = begin_list->next; return (begin_list); } diff --git a/c12/ex04/ft_list_push_back.c b/c12/ex04/ft_list_push_back.c index cfde9a8..db4fdb0 100644 --- a/c12/ex04/ft_list_push_back.c +++ b/c12/ex04/ft_list_push_back.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 17:18:46 by cacharle #+# #+# */ -/* Updated: 2019/07/09 17:18:48 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 18:04:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,15 @@ void ft_list_push_back(t_list **begin_list, void *data) { - if (*begin_list) - while (*begin_list->next) - *begin_list = *begin_list->next; - elem = ft_create_elem(data); - if (*begin_list) - *begin_list->next = elem; - else - *begin_list = elem; + t_list *new_rear; + + new_rear = ft_create_elem(data); + if (!*begin_list) + { + (*begin_list) = new_rear; + return ; + } + while ((*begin_list)->next) + *begin_list = (*begin_list)->next; + (*begin_list)->next = new_rear; } diff --git a/c12/ex05/ft_list_push_strs.c b/c12/ex05/ft_list_push_strs.c index 516fa7e..e56cd18 100644 --- a/c12/ex05/ft_list_push_strs.c +++ b/c12/ex05/ft_list_push_strs.c @@ -6,25 +6,29 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 17:18:23 by cacharle #+# #+# */ -/* Updated: 2019/07/09 17:52:44 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 18:34:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_list.h" +void list_push_front(t_list **begin_list, void *data) +{ + t_list *new_front; + + new_front = ft_create_elem(data); + new_front->next = *begin_list; + *begin_list = new_front; +} + t_list *ft_list_push_strs(int size, char **strs) { int i; - t_list elem; - t_list next; + t_list *list; - next = NULL; + list = NULL; i = 0; while (i < size) - { - elem = ft_create_elem(strs[i]); - elem->next = next; - next = elem; - } - return (&elem); + list_push_front(&list, strs[i++]); + return (list); } diff --git a/c12/ex06/ft_list_clear.c b/c12/ex06/ft_list_clear.c index 9a7cf5d..811469b 100644 --- a/c12/ex06/ft_list_clear.c +++ b/c12/ex06/ft_list_clear.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 17:53:02 by cacharle #+# #+# */ -/* Updated: 2019/07/09 18:02:09 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 18:20:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,11 @@ void ft_list_clear(t_list *begin_list, void (*free_fct)(void *)) { - t_list tmp; + t_list *tmp; - if (!begin_list) + if (begin_list == NULL) return ; - while (begin_list->next) + while (begin_list) { free_fct(begin_list->data); tmp = begin_list->next; diff --git a/c12/ex07/ft_list_at.c b/c12/ex07/ft_list_at.c index 5176a76..ad4b084 100644 --- a/c12/ex07/ft_list_at.c +++ b/c12/ex07/ft_list_at.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 17:56:31 by cacharle #+# #+# */ -/* Updated: 2019/07/09 18:01:18 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 18:36:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,7 @@ t_list *ft_list_at(t_list *begin_list, unsigned int nbr) { - int i; - - i = 0; - while (i < nbr && begin_list) - { + while (nbr-- > 0 && begin_list) begin_list = begin_list->next; - i++; - } return (begin_list); } diff --git a/c12/ex08/ft_list_reverse.c b/c12/ex08/ft_list_reverse.c index b650329..23d4339 100644 --- a/c12/ex08/ft_list_reverse.c +++ b/c12/ex08/ft_list_reverse.c @@ -6,17 +6,22 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 18:05:23 by cacharle #+# #+# */ -/* Updated: 2019/07/09 18:10:50 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 19:13:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ void ft_list_reverse(t_list **begin_list) { - t_list tmp; - while (*begin_list->next) - { - tmp = *begin_list->next; - + t_list *tmp; + t_list *prev; + prev = NULL; + tmp = (*begin_list)->next; + while (tmp != NULL) + { + (*begin_list)->next = prev; + prev = *begin_list; + *begin_list = tmp; + tmp = (*begin_list)->next; } } diff --git a/c12/ex09/ft_list.h b/c12/ex09/ft_list.h index 8b736b4..08a22a5 100644 --- a/c12/ex09/ft_list.h +++ b/c12/ex09/ft_list.h @@ -6,12 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ -/* Updated: 2019/07/09 14:42:55 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 17:11:04 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_LIST_H -#define FT_LIST_H +# define FT_LIST_H typedef struct s_list { diff --git a/c12/ex09/ft_list_foreach.c b/c12/ex09/ft_list_foreach.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex10/ft_list_foreach_if.c b/c12/ex10/ft_list_foreach_if.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex11/ft_list_find.c b/c12/ex11/ft_list_find.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex12/ft_list_remove_if.c b/c12/ex12/ft_list_remove_if.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex13/ft_list_merge.c b/c12/ex13/ft_list_merge.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex14/ft_list_sort.c b/c12/ex14/ft_list_sort.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex15/ft_list_reverse_fun.c b/c12/ex15/ft_list_reverse_fun.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex16/ft_sorted_list_insert.c b/c12/ex16/ft_sorted_list_insert.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/ex17/ft_sorted_list_merge.c b/c12/ex17/ft_sorted_list_merge.c new file mode 100644 index 0000000..e69de29 diff --git a/c12/main.c b/c12/main.c new file mode 100644 index 0000000..f0ed68a --- /dev/null +++ b/c12/main.c @@ -0,0 +1,82 @@ +#include +#include +#include "ex00/ft_list.h" +#include "ex00/ft_create_elem.c" +#include "ex01/ft_list_push_front.c" +#include "ex02/ft_list_size.c" +#include "ex03/ft_list_last.c" +#include "ex04/ft_list_push_back.c" +#include "ex05/ft_list_push_strs.c" +#include "ex06/ft_list_clear.c" +#include "ex07/ft_list_at.c" +/*#include "ex08/ft_list_reverse.c"*/ +/*#include "ex09/ft_list_foreach.c"*/ +/*#include "ex10/ft_list_foreach_if.c"*/ +/*#include "ex11/ft_list_find.c"*/ +/*#include "ex12/ft_list_remove_if.c"*/ +/*#include "ex13/ft_list_merge.c"*/ +/*#include "ex14/ft_list_sort.c"*/ +/*#include "ex15/ft_list_reverse_fun.c"*/ +/*#include "ex16/ft_sorted_list_insert.c"*/ +/*#include "ft_sorted_list_merge.c"*/ + +void ft_free(void *data); + +int main() +{ + t_list *list = NULL; + int a = 4; + void *data = &a; + + list = ft_create_elem(data); + printf("%d\n", *(int*)list->data); + + char b = 'g'; + data = &b; + ft_list_push_front(&list, data); + printf("%c\n", *(char*)list->data); + printf("%d\n", *(char*)list->next->data); + printf("%s\n", (char*)list->next->next); + + printf("size %d\n", ft_list_size(list)); + ft_list_push_front(&list, data); + printf("size %d\n", ft_list_size(list)); + + printf("last %d\n", *(int*)ft_list_last(list)->data); + + double c = 3.14; + data = &c; + ft_list_push_back(&list, data); + printf("list last %f\n", *(double*)ft_list_last(list)->data); + t_list *empty = NULL; + ft_list_push_back(&empty, data); + printf("empty last %f\n", *(double*)ft_list_last(list)->data); + + char **strs = malloc(sizeof(char*) * 4); + strs[0] = malloc(sizeof(char) * 32); + strs[1] = malloc(sizeof(char) * 32); + strs[2] = malloc(sizeof(char) * 32); + strs[3] = malloc(sizeof(char) * 32); + strcpy(strs[0], "bonjour"); + strcpy(strs[1], "je"); + strcpy(strs[2], "suis"); + strcpy(strs[3], "charles"); + t_list *list_strs = ft_list_push_strs(4, strs); + for (; list_strs; list_strs = list_strs->next) + printf("%s | ", (char*)list_strs->data); + + /*ft_list_clear(list_strs, &ft_free);*/ + + printf("\n--------------\n"); + printf("%d\n", *(int*)(ft_list_at(list, 0)->data)); + printf("%f\n", *(double*)(ft_list_at(list, 1)->data)); + printf("%s\n", (char*)ft_list_at(list, 2)); + + + +} + +void ft_free(void *data) +{ + free((char*)data); +} -- cgit