feat: added more lst functionality

This commit is contained in:
Zoëy Noort 2025-05-31 19:48:39 +02:00
parent 2708a86b5f
commit 0863951787
7 changed files with 100 additions and 0 deletions

13
srcs/lst/ft_lstclear.c Normal file
View file

@ -0,0 +1,13 @@
#include "libft.h"
#include <stdlib.h>
void ft_lstclear(t_list **lst)
{
t_list *tmp;
while (lst && *lst)
{
tmp = (*lst)->next;
free(*lst);
*lst = tmp;
}
}

6
srcs/lst/ft_lstempty.c Normal file
View file

@ -0,0 +1,6 @@
#include "libft.h"
int ft_lstempty(t_list *lst)
{
return (lst == NULL);
}

12
srcs/lst/ft_lstfind.c Normal file
View file

@ -0,0 +1,12 @@
#include "libft.h"
t_list *ft_lstfind(t_list *lst, void *data, int (*cmp)(void *, void *))
{
while (lst)
{
if (cmp(lst->data, data) == 0)
return (lst);
lst = lst->next;
}
return (NULL);
}

12
srcs/lst/ft_lstget.c Normal file
View file

@ -0,0 +1,12 @@
#include "libft.h"
t_list *ft_lstget(t_list *lst, int index)
{
int i = 0;
while (lst && i < index)
{
lst = lst->next;
i++;
}
return ((i == index) ? lst : NULL);
}

30
srcs/lst/ft_lstinsert.c Normal file
View file

@ -0,0 +1,30 @@
#include "libft.h"
int ft_lstinsert(t_list **lst, t_list *node, int index)
{
t_list *curr = *lst;
t_list *prev = NULL;
int i = 0;
if (index < 0 || !node)
return (-1);
if (index == 0)
{
node->next = *lst;
*lst = node;
return (0);
}
while (curr && i < index)
{
prev = curr;
curr = curr->next;
i++;
}
if (i == index)
{
prev->next = node;
node->next = curr;
return 0;
}
return (-1);
}

10
srcs/lst/ft_lstiter.c Normal file
View file

@ -0,0 +1,10 @@
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
while (lst)
{
f(lst->data);
lst = lst->next;
}
}

17
srcs/lst/ft_lstreverse.c Normal file
View file

@ -0,0 +1,17 @@
#include "libft.h"
void ft_lstreverse(t_list **lst)
{
t_list *prev = NULL;
t_list *curr = *lst;
t_list *next;
while (curr)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*lst = prev;
}