ft_lstdelone - chanhl22/libft GitHub Wiki
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* ft_lstdelone.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: chanhlee <[email protected].> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2021/01/29 15:53:49 by chanhlee #+# #+# */
9 /* Updated: 2021/01/30 00:09:11 by chanhlee ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12
13 #include "libft.h"
14
15 void ft_lstdelone(t_list *lst, void (*del)(void *))
16 {
17 if (!del) // lst ๋ del ์ด ๋น์ด์์ ๊ฒฝ์ฐ, ํจ์๋ฅผ ์ข
๋ฃํจ.
18 return ;
19 if (lst)
20 {
21 del(lst->content); // del = ๋
ธ๋๋ฅผ ์ญ์ ํ ๋ content์ ์ ์ฉํ ํจ์,
22 free(lst); // ๊ทธ๋ฆฌ๊ณ ์์๋ฅผ free์ํจ๋ค (ํจ์๊ฐ ๊ทธ๋ ๊ฒ ์ฐ์ด๋๊ฑฐ๋๊น)
23 lst = NULL; // free ํ์๋ ๋ฉ๋ชจ๋ฆฌ์๋ ์ด์ ๊ฐ์ด ๋จ์์๊ธฐ ๋๋ฌธ์ 0์ผ๋ก ์ด๊ธฐํ๋ฅผ ํด์ฃผ๋ ๊ฒ์ด ์ข์
24 }
25 }
- del = ๋
ธ๋๋ฅผ ์ญ์ ํ ๋ content์ ์ ์ฉํ ํจ์
content๊ฐ ๋์ ํ ๋น๋ ํฌ์ธํฐ์ธ ๊ฒฝ์ฐ ๋ณดํต free๋ฅผ ์ ๋ฌํด์ฃผ๋ ๊ฒ ๊ฐ์ต๋๋ค
ft_lstdelone(lst, free); // del๋ก free๋ฅผ ์ฌ์ฉ
ft_lstdelone(lst, myfunction); // del๋ก ๋ด๊ฐ๋ง๋ myfunction ์ฌ์ฉ
๊ตฌ์กฐ์ฒด ์์ void *content์ ์๋ฃํ์ด ์ด๋ค๊ฒ ๋ค์ด ์ฌ์ง ๋ชจ๋ฆ, int๊ฐ ๋ ์๋ ์๊ณ charํ์ด ๋ ์๋ ์์ง๋ง ์ฐ๊ฒฐ๋ฆฌ์คํธ์ content ๋ก ๋ค์ด๊ฐ์๋ ์๋ค.
- ๋ง์ฝ์ del๋งค๊ฐ๋ณ์์ free๊ฐ ๋ค์ด๊ฐ๋ค๋ฉด free(lst->content) == del(lst->content)
- ๊ทธ๋ฌ๋๊น delํจ์ํฌ์ธํฐ๋ก ๋
ธ๋์ ์์๋ฅผ ์ ์ฉํ๋ค.
- ๊ทธ๋ฆฌ๊ณ ์์๋ฅผ free์ํจ๋ค (ํจ์๊ฐ ๊ทธ๋ ๊ฒ ์ฐ์ด๋๊ฑฐ๋๊น)