fix: lst -> slist

This commit is contained in:
Zoëy Noort 2025-05-31 20:17:01 +02:00
parent 93da2c9ca6
commit 14adc54215
16 changed files with 48 additions and 46 deletions

View file

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

View file

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

View file

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

12
srcs/slist/ft_slist_get.c Normal file
View file

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

View file

@ -0,0 +1,30 @@
#include "libft.h"
int ft_slist_insert(t_slist **lst, t_slist *node, int index)
{
t_slist *curr = *lst;
t_slist *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);
}

View file

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

14
srcs/slist/ft_slist_new.c Normal file
View file

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

23
srcs/slist/ft_slist_pop.c Normal file
View file

@ -0,0 +1,23 @@
#include "libft.h"
t_slist *ft_list_pop(t_slist **lst)
{
t_slist *current;
t_slist *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;
}

View file

@ -0,0 +1,22 @@
#include "libft.h"
void ft_slist_push(t_slist **lst, t_slist *node)
{
t_slist *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;
}

View file

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

24
srcs/slist/ft_slist_rm.c Normal file
View file

@ -0,0 +1,24 @@
#include "libft.h"
void ft_slist_rm(t_slist **lst, void *data, int (*cmp)(void *, void *), void (*del)(void *))
{
t_slist *curr = *lst;
t_slist *prev = NULL;
while (curr)
{
if (cmp(curr->data, data) == 0)
{
if (prev)
prev->next = curr->next;
else
*lst = curr->next;
if (del)
del(curr->data);
free(curr);
return;
}
prev = curr;
curr = curr->next;
}
}

View file

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

View file

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

View file

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