Skip to content

Commit

Permalink
ex10-12
Browse files Browse the repository at this point in the history
  • Loading branch information
InesZenkri committed Jan 13, 2024
1 parent 5310ee6 commit 551beb4
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
26 changes: 26 additions & 0 deletions c12/ex10/ft_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: izenkri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 12:59:58 by zenkri #+# #+# */
/* Updated: 2024/01/13 19:39:40 by izenkri ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef FT_LIST_H
# define FT_LIST_H

# include <stdlib.h>

typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;

t_list *ft_create_elem(void *data);

#endif
24 changes: 24 additions & 0 deletions c12/ex10/ft_list_foreach_if.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list_foreach_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: izenkri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 12:59:58 by zenkri #+# #+# */
/* Updated: 2024/01/13 19:40:25 by izenkri ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_list.h"

void ft_list_foreach_if(t_list *begin_list,
void (*f)(void *), void *data_ref, int (*cmp)())
{
while (begin_list)
{
if ((*cmp)(begin_list->data, data_ref) == 0)
(*f)(begin_list->data);
begin_list = begin_list->next;
}
}
26 changes: 26 additions & 0 deletions c12/ex11/ft_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: izenkri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 12:59:58 by zenkri #+# #+# */
/* Updated: 2024/01/13 19:41:20 by izenkri ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef FT_LIST_H
# define FT_LIST_H

# include <stdlib.h>

typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;

t_list *ft_create_elem(void *data);

#endif
24 changes: 24 additions & 0 deletions c12/ex11/ft_list_find.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list_find.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: izenkri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 12:59:58 by zenkri #+# #+# */
/* Updated: 2024/01/13 19:41:44 by izenkri ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_list.h"

t_list *ft_list_find(t_list *begin_list, void *data_ref, int (*cmp)())
{
while (begin_list)
{
if ((*cmp)(begin_list->data, data_ref) == 0)
return (begin_list);
begin_list = begin_list->next;
}
return (0);
}
27 changes: 27 additions & 0 deletions c12/ex12/ft_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: izenkri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 12:59:58 by zenkri #+# #+# */
/* Updated: 2024/01/13 19:43:22 by izenkri ### ########.fr */
/* */
/* ************************************************************************** */


#ifndef FT_LIST_H
# define FT_LIST_H

# include <stdlib.h>

typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;

t_list *ft_create_elem(void *data);

#endif
40 changes: 40 additions & 0 deletions c12/ex12/ft_list_remove_if.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list_remove_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: izenkri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 12:59:58 by zenkri #+# #+# */
/* Updated: 2024/01/13 19:42:55 by izenkri ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_list.h"

void ft_list_remove_if(t_list **begin_list,
void *data_ref, int (*cmp)(), void (*free_fct)(void *))
{
t_list *list;
t_list *tmp;

list = *begin_list;
while (list && list->next)
{
if ((*cmp)(list->next->data, data_ref) == 0)
{
tmp = list->next;
list->next = list->next->next;
(*free_fct)(tmp->data);
free(tmp);
}
list = list->next;
}
list = *begin_list;
if (list && (*cmp)(list->data, data_ref) == 0)
{
*begin_list = list->next;
(*free_fct)(list->data);
free(list);
}
}

0 comments on commit 551beb4

Please sign in to comment.