공략집
함수 포인터
-
임의의 함수 cmp를 만든다.
-
넣을 때 다음과 같이 넣으면 된다.
ft_list_remove_if(list, vdata, cmp);
문제
Assignment name : ft_list_remove_if
Expected files : ft_list_remove_if.c
Allowed functions: free
--------------------------------------------------------------------------------
Write a function called ft_list_remove_if that removes from the
passed list any element the data of which is "equal" to the reference data.
It will be declared as follows :
void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)());
cmp takes two void* and returns 0 when both parameters are equal.
You have to use the ft_list.h file, which will contain:
$>cat ft_list.h
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
$>
코드
c
#include <stdlib.h>
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)())
{
t_list *cur = *begin_list;
t_list *before = 0;
while (cur)
{
if ((*cmp)(cur->data, data_ref) == 0)
{
if (before == 0)
{
*begin_list = cur->next;
free(cur);
cur = *begin_list;
}
else
{
before->next = cur->next;
free(cur);
cur = before->next;
}
}
else
{
before = cur;
cur = cur->next;
}
}
}
#include <stdio.h>
void ft_printlist(t_list *lst)
{
while (lst)
{
printf("data : %d\n", (int)lst->data);
lst = lst->next;
}
}
t_list *lst_new(void *data)
{
t_list *lst;
lst = (t_list*)malloc(sizeof(t_list));
lst->next = 0;
lst->data = data;
return (lst);
}
int cmp(void *a, void *b)
{
if ((int)a == (int)b)
return (0);
else
return (1);
}
int main(void)
{
t_list *lst;
lst = lst_new((void *)5);
lst->next = lst_new((void *)3);
lst->next->next = lst_new((void *)30);
lst->next->next->next = lst_new((void *)6);
lst->next->next->next->next = lst_new((void *)9);
lst->next->next->next->next->next = lst_new((void *)5);
lst->next->next->next->next->next->next = lst_new((void *)2);
lst->next->next->next->next->next->next->next = lst_new((void *)4);
ft_printlist(lst);
ft_list_remove_if(&lst, (void *)5, &cmp);
printf("---------------------------------------\n");
ft_printlist(lst);
return (0);
}