feat: added more lst functionality
This commit is contained in:
parent
2708a86b5f
commit
0863951787
7 changed files with 100 additions and 0 deletions
30
srcs/lst/ft_lstinsert.c
Normal file
30
srcs/lst/ft_lstinsert.c
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue