add: ft_list_push_front

This commit is contained in:
starnakin 2024-09-05 18:14:20 +02:00
parent b1a86b85fe
commit eadf7cde62
3 changed files with 60 additions and 2 deletions

View File

@ -13,3 +13,4 @@ char *ft_strdup(const char *s);
ssize_t ft_read(int fildes, void *buf, size_t nbyte); ssize_t ft_read(int fildes, void *buf, size_t nbyte);
int ft_list_size(t_list *begin_list); int ft_list_size(t_list *begin_list);
void ft_list_push_front(t_list **begin_list, void *data);

View File

@ -0,0 +1,27 @@
extern malloc
section .text
global ft_list_push_front
ft_list_push_front:
push rdi
push rsi
mov rdi, 16
call malloc wrt ..plt
cmp rax, 0
je out
pop rsi
pop rdi
mov rdx, [rdi]
mov [rax + 0], rsi ; .data = arg#1
mov [rax + 8], rdx ; .next = *arg#0
mov [rdi], rax
out:
ret

View File

@ -103,6 +103,7 @@ t_list *create_list(size_t len)
for (size_t i = 0; i < len; i++) for (size_t i = 0; i < len; i++)
{ {
current->next = malloc(sizeof(t_list)); current->next = malloc(sizeof(t_list));
current->data = (void *) i;
if (current->next == NULL) if (current->next == NULL)
{ {
destroy_list(current, NULL); destroy_list(current, NULL);
@ -120,7 +121,7 @@ t_list *create_list(size_t len)
void multiple_test_list_size() void multiple_test_list_size()
{ {
size_t len; size_t len;
t_list **root; t_list *root;
size_t specific_size[nb_specific_test] = {0}; size_t specific_size[nb_specific_test] = {0};
srand(time(NULL)); srand(time(NULL));
@ -137,6 +138,31 @@ void multiple_test_list_size()
} }
} }
void multiple_test_push_front()
{
size_t len;
t_list *root;
srand(time(NULL));
for (size_t i = 0; i < nb_random_test; i++)
{
len = rand() % 100;
root = create_list(len);
if (root == NULL)
return;
ft_list_push_front(&root, (void *) 101);
printf("data: ");
test_size_t((size_t) root->data, 101);
printf(" data-next: ");
test_size_t((size_t) root->next->data, 0);
printf(" list-size: ");
test_int(len + 1, ft_list_size(root));
printf("\n");
}
}
int main() int main()
{ {
printf("STRLEN\n"); printf("STRLEN\n");
@ -165,4 +191,8 @@ int main()
printf("FT_LIST_SIZE\n"); printf("FT_LIST_SIZE\n");
multiple_test_list_size(); multiple_test_list_size();
printf("\n"); printf("\n");
printf("FT_LIST_PUSH_FRONT\n");
multiple_test_push_front();
printf("\n");
} }