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

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);
}