feat: added lst structure

This commit is contained in:
Zoëy Noort 2025-05-31 19:36:07 +02:00
parent f3377656e4
commit 3287cd402f
6 changed files with 93 additions and 0 deletions

14
srcs/lst/ft_lstnew.c Normal file
View file

@ -0,0 +1,14 @@
#include "libft.h"
t_list *ft_lstnew(void *data)
{
t_list *node = (t_list *)malloc(sizeof(t_list));
if (!node)
return (NULL);
node->data = data;
node->next = NULL;
return node;
}

23
srcs/lst/ft_lstpop.c Normal file
View file

@ -0,0 +1,23 @@
#include "libft.h"
t_list *ft_lstpop(t_list **lst)
{
t_list *current;
t_list *prev;
if (!lst || !*lst)
return NULL;
current = *lst;
if (current->next == NULL)
{
*lst = NULL;
return current;
}
while (current->next)
{
prev = current;
current = current->next;
}
prev->next = NULL;
return current;
}

22
srcs/lst/ft_lstpush.c Normal file
View file

@ -0,0 +1,22 @@
#include "libft.h"
void ft_lstpush(t_list **lst, t_list *node)
{
t_list *current;
if (!lst || !node)
return;
node->next = NULL;
if (*lst == NULL)
{
*lst = node;
return;
}
current = *lst;
while (current->next != NULL)
current = current->next;
current->next = node;
}

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

@ -0,0 +1,13 @@
#include "libft.h"
t_list *ft_lstshift(t_list **lst)
{
t_list *first;
if (!lst || !*lst)
return NULL;
first = *lst;
*lst = first->next;
first->next = NULL;
return first;
}

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

@ -0,0 +1,12 @@
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int size = 0;
while (lst)
{
size++;
lst = lst->next;
}
return size;
}

9
srcs/lst/ft_lstunshift.c Normal file
View file

@ -0,0 +1,9 @@
#include "libft.h"
void ft_lstunshift(t_list **lst, t_list *node)
{
if (!lst || !node)
return;
node->next = *lst;
*lst = node;
}