aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--functions_reference/ref_ft_list_size.c2
-rw-r--r--helper_list.c88
-rw-r--r--libasm_test.h6
3 files changed, 92 insertions, 4 deletions
diff --git a/functions_reference/ref_ft_list_size.c b/functions_reference/ref_ft_list_size.c
index 7473bad..411f0f5 100644
--- a/functions_reference/ref_ft_list_size.c
+++ b/functions_reference/ref_ft_list_size.c
@@ -13,5 +13,3 @@ ref_ft_list_size(t_list *begin_list)
}
return counter;
}
-
-
diff --git a/helper_list.c b/helper_list.c
new file mode 100644
index 0000000..71e69fa
--- /dev/null
+++ b/helper_list.c
@@ -0,0 +1,88 @@
+#include "libasm_test.h"
+
+static t_list*
+create_elem(int data)
+{
+ int *data_ptr = malloc(sizeof(int));
+ if (data_ptr == NULL)
+ return (NULL);
+ t_list *new = malloc(sizeof(t_list));
+ if (new == NULL)
+ return NULL;
+ new->data = data_ptr;
+ new->next = NULL;
+ return new;
+}
+
+static void
+push_front(t_list **lst_ptr, t_list *new_front)
+{
+ if (lst_ptr == NULL || new_front == NULL)
+ return ;
+ new_front->next = *lst_ptr;
+ *lst_ptr = new_front;
+}
+
+static t_list*
+reverse(t_list *list)
+{
+ if (list == NULL || list->next == NULL)
+ return list;
+ t_list *tmp = reverse(list->next);
+ list->next->next = list;
+ list->next = NULL;
+ return tmp;
+}
+
+t_list*
+list_from_format(char *fmt)
+{
+ t_list *head = NULL;
+
+ while (*fmt)
+ {
+ int n = ft_atoi(*fmt);
+ t_list *elem = create_elem(n);
+ push_front(&head, elem);
+ while (isdigit(*fmt))
+ fmt++;
+ if (*fmt)
+ fmt++;
+ }
+ return head;
+}
+
+t_list*
+list_dup(t_list *list)
+{
+ t_list *dup_head = NULL;
+
+ while (list != NULL)
+ {
+ t_list *tmp = create_elem(*(int*)list->data);
+ push_front(&dup_head, tmp);
+ }
+ return reverse(dup_head);
+}
+
+int
+list_cmp(t_list *l1, t_list l2)
+{
+ if (l1 == NULL && l2 == NULL)
+ return 0;
+ if (l1 == NULL)
+ return -1;
+ if (l2 == NULL)
+ return 1;
+ if (*(int*)l1->data != *(int*)l2->data)
+ return *(int*)l1->data - *(int*)l2->data;
+ return list_cmp(l1->next, l2->next);
+}
+
+void
+list_print(t_list *list)
+{
+ while (list != NULL)
+ printf("[%d] -> ", *(int*)list->data);
+ printf("(null)\n");
+}
diff --git a/libasm_test.h b/libasm_test.h
index 3076372..d31f699 100644
--- a/libasm_test.h
+++ b/libasm_test.h
@@ -46,7 +46,8 @@ ft_list_size(t_list *begin_list);
void
ft_list_sort(t_list **begin_list, int (*cmp)());
void
-ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void *));
+ft_list_remove_if(t_list **begin_list, void *data_ref,
+ int (*cmp)(), void (*free_fct)(void *));
/*
* test_functions
@@ -99,7 +100,8 @@ ref_ft_list_size(t_list *begin_list);
void
ref_ft_list_sort(t_list **begin_list, int (*cmp)());
void
-ref_ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void *));
+ref_ft_list_remove_if(t_list **begin_list, void *data_ref,
+ int (*cmp)(), void (*free_fct)(void *));
/*
* segfault tester