Const 키워드란
constant의 약자로 “변함없는” 이란 뜻으로 변수 앞에 붙이면 값을 변경하지 못하도록 한다. 해당 변수를 상수로 취급하게 된다.
메모리 누수란
동적으로 할당한 메모리가 free될 수 없 는 상태가 된것.
발생시 문제점 : 누수가 지속적으로 일어날 경우 메모리 사용량이 증가되고, 결국 시스템의 메모리가 부족해져 운영체제가 프로그램을 강제로 종료시키거나 메모리 할당에 실패할 수 있다.
make re
re fclean all의 경우 순서가 뒤바뀌는 경우가 있을수 있다.
연결리스트 구현시 이중 포인터를 사용하는 이유
단일 연결 리스트에서 삽입과 삭제를 통해 head 포인터의 값을 변화시킬 수 있다.
이때, 호출 함수의 포인터 변수가 참조하는 객체를 피호출 함수에서 바꾸고자 할 경우 이중 포인터를 사용하면 된다.
t_list **lst는 t_list포인터의 주소를 가리키는 값
t_list *lst 변수가 담고 있는 값은 t_list 의 주소
t_list lst변수가 담고 있는 겂은 어떤 리스트의 첫번째 주소 즉, lst는 head의 주소
*lst == null은 빈 리스트
lst == null은 리스트 자체가 존재하지 않는다는 것.
ascii to int 함수
맨 앞의 공백문자는 스킵함, 부호는 하나만 있어야함. 중간에 '0' ~ '9' 외의 문자가 있으면 멈춤.
atoi의 반환값은 (int)strtol(str, (char **)NULL, 10); 와 같다.
--> long 범위에서 int형 범위를 벗어난 경우, 오버플로된 값을 그대로 리턴
--> long 범위를 벗어난 경우, 음수일 땐 0 양수일 땐 -1을 리턴함. (이 부분은 구현을 해야하는 가에 대해서 많은 말들이 있었다. 기존의 atoi함수를 완벽하게 구현해야 한다면, 구현하는게 맞다고 생각했지만, 이번 과제는 내가 사용할 함수들을 직접! 구현하는 과제라고 생각해 int범위까지만 변환될 수 있게만 해도 무방할 것이라 생각했다.)
(❗️맥에서는 long범위와 longlong의 범위가 같다.)
int ft_atoi(const char *str)
{
int index;
int sign;
int num;
index = 0;
sign = 1;
num = 0;
while ((str[index] >= 9 && str[index] <= 13) || str[index] == 32)
index++;
if (str[index] == '+' || str[index] == '-')
{
if (str[index] == '-')
sign = -1;
index++;
}
while (str[index] >= '0' && str[index] <= '9')
{
num = num * 10 + (str[index] - '0');
index++;
}
return (sign * num);
}
/*
#include <stdio.h>
int main(void)
{
printf("a int max 2147483647 : %d\n", atoi("2147483647"));
printf("f int max 2147483647 : %d\n", ft_atoi("2147483647"));
printf("a int min -2147483648 : %d\n", atoi("-2147483648"));
printf("f int min -2147483648 : %d\n", ft_atoi("-2147483648"));
printf("a int over 9999999999 : %d\n", atoi("9999999999"));
printf("f int over 9999999999 : %d\n", ft_atoi("9999999999"));
printf("a int under -9999999999 : %d\n", atoi("-9999999999"));
printf("f int under -9999999999 : %d\n", ft_atoi("-9999999999"));
printf("a long long plus 92233720368547758 : %d\n", atoi("92233720368547758"));
printf("f long long plus 92233720368547758 : %d\n", ft_atoi("92233720368547758"));
printf("a long long minus -92233720368547758 : %d\n", atoi("-92233720368547758"));
printf("f long long minus -92233720368547758 : %d\n", ft_atoi("-92233720368547758"));
printf("a long long max 9223372036854775807 : %d\n", atoi("9223372036854775807"));
printf("f long long max 9223372036854775807 : %d\n", ft_atoi("9223372036854775807"));
printf("a long long min -9223372036854775808 : %d\n", atoi("–9223372036854775808"));
printf("f long long min -9223372036854775808 : %d\n", ft_atoi("–9223372036854775808"));
printf("a long long max / 10 922337203685477580 : %d\n", atoi("922337203685477580"));
printf("f long long max / 10 922337203685477580 : %d\n", ft_atoi("922337203685477580"));
printf("a long long max / 10 + 1 922337203685477581 : %d\n", atoi("922337203685477581"));
printf("f long long max / 10 + 1 922337203685477581 : %d\n", ft_atoi("922337203685477581"));
printf("a long long max / 10 + 4 922337203685477584 : %d\n", atoi("922337203685477584"));
printf("f long long max / 10 + 4 922337203685477584 : %d\n", ft_atoi("922337203685477584"));
printf("a long long max / 10 + 5 922337203685477585 : %d\n", atoi("922337203685477585"));
printf("f long long max / 10 + 5 922337203685477585 : %d\n", ft_atoi("922337203685477585"));
printf("a long long max - 1 922337203685477586 : %d\n", atoi("922337203685477586"));
printf("f long long max - 1 922337203685477586 : %d\n", ft_atoi("922337203685477586"));
printf("a long long max + 1 9223372036854775808 : %d\n", atoi("9223372036854775808"));
printf("f long long max + 1 9223372036854775808 : %d\n", ft_atoi("9223372036854775808"));
printf("a long long max + 2 9223372036854775809 : %d\n", atoi("9223372036854775809"));
printf("f long long max + 2 9223372036854775809 : %d\n", ft_atoi("9223372036854775809"));
printf("a long long max + 10 9223372036854775817 : %d\n", atoi("9223372036854775817"));
printf("f long long max + 10 9223372036854775817 : %d\n", ft_atoi("9223372036854775817"));
printf("a long long min - 1 –9223372036854775809 : %d\n", atoi("–9223372036854775809"));
printf("f long long min - 1 –9223372036854775809 : %d\n", ft_atoi("–9223372036854775809"));
printf("a long long min - 2 –9223372036854775810 : %d\n", atoi("–9223372036854775810"));
printf("f long long min - 2 –9223372036854775810 : %d\n", ft_atoi("–9223372036854775810"));
printf("a long long min - 10 –9223372036854775818 : %d\n", atoi("–9223372036854775818"));
printf("f long long min - 10 –9223372036854775818 : %d\n", ft_atoi("–9223372036854775818"));
printf("a long long over 9223379876854775807 : %d\n", atoi("9223379876854775807"));
printf("f long long over 9223379876854775807 : %d\n", ft_atoi("9223379876854775807"));
printf("a long long under -9223379876854775807 : %d\n", atoi("-9223379876854775807"));
printf("f long long under -9223379876854775807 : %d\n", ft_atoi("-9223379876854775807"));
printf("a long long half 4611686018427387903 : %d\n", atoi("4611686018427387903"));
printf("f long long half 4611686018427387903 : %d\n", ft_atoi("4611686018427387903"));
printf("a zero 0 : %d\n", atoi("0"));
printf("f zero 0 : %d\n", ft_atoi("0"));
printf("a empty : %d\n", atoi(""));
printf("f empty : %d\n", ft_atoi(""));
return (0);
}*/
포인터 s부터 값을 n byte만큼 0 으로 초기화 해 주는 함수이다. s는 함수 내에서 unsigned char의 형태로 캐스팅 되어 사용된다.
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
size_t i;
unsigned char *p;
p = (unsigned char *)s;
i = 0;
while (i < n)
p[i++] = 0;
}
size바이트 크기의 변수를 count 개 저장할수 있는 크기를 할당하고 0으로 초기화
return value : 성공시 할당된 메모리의 포인터, 실패시 NULL
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
unsigned char *p;
size_t i;
p = (unsigned char *)malloc(sizeof(char) * (count * size));
if (p == 0)
return (0);
i = 0;
while (i < size * count)
{
p[i++] = 0;
}
return ((void *)p);
}
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
unsigned char *p;
size_t i;
p = (unsigned char *)malloc(sizeof(char) * (count * size));
if (p == 0)
return (0);
i = 0;
while (i < size * count)
{
p[i++] = 0;
}
return ((void *)p);
}
int isalnum( int _C );
_C가 알파벳이거나 숫자인지를 판별하게 됩니다.
알파벳이거나 숫자이면 0이 아닌 값을 리턴합니다. 알파벳이거나 숫자가 아니면 0을 리턴합니다.
int ft_isalnum(int c)
{
if (c >= 60 && c <= 71)
return (1);
else if (c >= 101 && c <= 132)
return (1);
else if (c >= 141 && c <= 172)
return (1);
else
return (0);
}
int isalpha(int c);
_C가 소문자 알파벳인지 대문자 알파벳인지를 판별하게 됩니다.
매개변수 : C언어에서 아스키 코드에 해당하는 문자들은 숫자로 표현이 되고, 문자를 넣으면 자동으로 아스키 코드에 있는 숫자로 들어가기 때문에 int 타입이긴 하지만 'a', 'A', '1' 등을 집어 넣어도됩니다.즉, 'a' 와 같이 char 타입으로 집어 넣어도 자동으로 int 타입으로 형변환 되어서 들어가게 됩니다. 아스키 코드 표를 참고하면 'a'는 자동으로 숫자 97로 형변환되어 들어가게 됩니다.
반환형 : 알파벳일 경우 1, 아닐 경우 0을 반환합니다.
int ft_isalpha(int c)
{
if (c >= 'A' && c <= 'Z')
return (1);
else if (c >= 'a' && c <= 'z')
return (1);
else
return (0);
}
아스키에 포함 여부 판별 함수
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}
숫자인지 판별해주는 함수
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}
printable 한지 판별해주는 함수
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}
int형 숫자를 문자형 ascii로 변환하는 함수
아래의 코드는 음수인 n이 들어왓을 때 양수로 바꾸어 준 뒤 진행되는데, 이러한 경우 INT_MIN이 들어왓을 경우 양수로 바꾸어 주었을 때,
#include "libft.h"
#include <stdio.h>
static int detec_len(long long nbr)
{
int len;
len = 0;
if (nbr < 0)
{
nbr *= -1;
len++;
}
while (nbr >= 10)
{
nbr = nbr / 10;
len++;
}
len++;
return (len);
}
char *ft_itoa(int n)
{
long long nbr;
int len;
char *arr;
nbr = (long long)n;
len = detec_len(nbr);
arr = (char *)malloc(sizeof(char) * (len + 1));
if (!arr)
return (NULL);
if (nbr < 0)
{
nbr *= -1;
arr[0] = '-';
}
if (nbr == 0)
arr[0] = '0';
arr[len] = '\0';
while (nbr != 0)
{
arr[len - 1] = (nbr % 10) + '0';
nbr /= 10;
len--;
}
return (arr);
}
/*
int main(void)
{
printf("%s", ft_itoa(123123123123123123123123123123123));
}
*/
void *memchr(const void *s, int c, size_t n);
s에서 n바이트 중에서 c를 찾는 함수.
Return value : 처음 발견된 위치의 포인터, 발견하지 못하면 NULL
unsigned char로 캐스팅 하는 이유
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char *p;
size_t i;
p = (unsigned char *)s;
i = 0;
while (i < n)
{
if (p[i] == (unsigned char *)c)
return ((void *)s[i]);
i++;
}
return (0);
}
두 개의 메모리 블럭을 비교
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *str1;
unsigned char *str2;
size_t i;
i = 0;
if (n == 0)
return (0);
str1 = (unsigned char *)s1;
str2 = (unsigned char *)s2;
while (i < n)
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (0);
}
Src가 가리키는 곳 부터 Size 바이트 만큼 Dst이 가리키는 곳으로 복사
메모리 영역 src로부터 n바이트를 메모리 영역 dst에 복사한다.
dst와 src의 메모리 공간이 겹쳐져 있는 경우는 undefined. behavior이다.
겹쳐져 있는 경우 memmove()를 사용.!
void *memcpy(void *Dst,const void *Src,size_t Size)
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
size_t i;
unsigned char *c_dst;
unsigned char *c_src;
i = 0;
c_dst = (unsigned char *)dst;
c_src = (unsigned char *)src;
if (c_dst == c_src)
return (c_dst);
while (i < n)
{
c_dst[i] = c_src[i];
i++;
}
return (dst);
}
dest의 주소가 src보다 뒤에 있을 경우 문제가 발생하기 때문에 별도의 처리가 필요
→ 이 경우 뒤에서 부터 복사 진행
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t num)
{
size_t i;
i = 0;
if (!(unsigned char *)dest && !(unsigned char *)src)
return (dest);
if ((unsigned char *)dest >= (unsigned char *)src)
{
while (i < num)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
}
}
else if ((unsigned char *)dest < (unsigned char *)src)
{
while (i < num)
{
((unsigned char *)dest)[num - 1 - i] =
((unsigned char *)src)[num - 1 - i];
i++;
}
}
return (dest);
}
len 바이트 만큼 b주소에 c를 복사
#include "libft.h"
void *ft_memset(void *b, int c, size_t len)
{
unsigned char *p;
size_t i;
set = (unsigned char)c;
p = (unsigned char *)b;
i = 0;
while (i < len)
p[i++] = set;
return (b);
}
int tolower(int c)
c가 알파벳 대문자인 경우 소문자로 바꿔서 리턴
int ft_tolower(int c)
{
if ('A' <= c && c >= 'Z')
return (c + 32);
return (c);
}
int toupper(int c)
c가 알파벳 소문자인 경우 대문자로 바꿔서 리턴
int ft_toupper(int c)
{
if ('a' <= c && c >= 'z')
return (c - 32);
return (c);
}
문자열의 길이를 반환해주는 함수
size_t ft_strlen(const char *str)
{
size_t a;
a = 0;
while (str[a] != '\0')
a++;
return (a);
}
문장에서 문자 찾기
문장에서 뒤에서부터 문자 찾기
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
i = ft_strlen(s);
while (i >= 0)
{
if (s[i] == (unsigned char)c)
return ((char *)s + i);
i--;
}
return (0);
}
문장에의 n 범위내에서 str찾기
needle이 빈 문자열이면 haystack의 첫 주소가 반환.
못 찾을 경우 NULL리턴
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
j = 0;
i = 0;
if (*needle == '\0')
return ((char *)haystack);
while (i < len && haystack[i] != '\0')
{
if (haystack[i] == needle[0])
{
j = 1;
while (needle[j] != '\0' && i + j < len
&& haystack[i + j] == needle[j])
j++;
if (needle[j] == '\0')
return ((char *)&haystack[i]);
}
i++;
}
return (NULL);
}
/*#include <stdio.h>
int main()
{
const char s1[] = "hello";
const char s2[] = "el";
char *str;
str = ft_strnstr(s1, s2, 3);
printf("%s",str);
}*/
문자열을 자를때 사용하는 함수
char *ft_substr(char const *s, unsigned int start, size_t len);
s: 부분 문자열 (substring) 을 생성할 원본 문자열
start: 부분 문자열의 맨 처음 인덱스
len: 부분 문자열의 최대 길이
Return Value
부분 문자열. 할당 실패 시, NULL
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t j;
char *sub;
i = 0;
j = 0;
if (!s)
return (NULL);
if (!(sub = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
while (s[i])
{
if (i >= start && j < len)
{
sub[j] = s[i];
j++;
}
i++;
}
sub[j] = '\0';
return (sub);
}
## MKO가 발생하는 코드
문자열의 끝을 확인하지 않음:
문자열 s가 null 종료되어있는지 확인하지 않고 while 루프를 사용하여 문자열을 반복하므로 s가 null
종료되어 있지 않으면 예기치 않은 결과가 발생할 수 있습니다.
할당된 메모리의 해제:
이 코드는 메모리를 동적으로 할당하고 있지만 할당된 메모리를 해제하지 않습니다.
이는 메모리 누수(memory leak)를 발생시킬 수 있으며 장기적으로는 시스템에 부정적인 영향을 미칠 수
있습니다.
문자열 길이 검사:
문자열 s의 길이를 검사하지 않으므로, start 매개변수가 s의 길이보다 크거나 같은 경우,
해당 함수는 빈 문자열을 반환합니다. 따라서 문자열 s의 길이를 검사하여 해당 문자열이 start 인덱스보다
길지 않은 경우 이를 처리해야 합니다.
마지막 문자열 문자 포함:
반환되는 하위 문자열(substring)에 null 종료 문자('\0')가 포함되지 않는 문제가 있습니다.
이것은 문자열 함수를 사용하여 하위 문자열을 사용할 때 예기치 않은 결과를 초래할 수 있습니다.
부호 없는 정수와 부호 있는 정수의 혼용:
start 매개변수는 부호 없는 정수(unsigned int)로 선언되어 있지만 i 변수는 size_t형으로 선언되어
있으므로 부호 없는 정수와 부호 있는 정수의 혼용이 발생합니다. 이는 컴파일러에 따라 다른 동작을 할 수
있으므로 이 문제를 수정해야 합니다.
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t s_len;
char *sub;
i = 0;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
return (ft_strdup(""));
if (len > s_len - start)
len = s_len - start;
sub = (char *)malloc(sizeof(char) * (len + 1));
if (!sub)
return (NULL);
while (i < len && s[start + i])
{
sub[i] = s[start + i];
i++;
}
sub[i] = '\0';
return (sub);
}
/*#include <stdio.h>
int main(void)
{
char *str;
str = ft_substr("hello, 42seoul!", 7, 123131231);
printf("%s", str);
return (0);
}
*/
## MKO가 발생하지 않는 코드
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t s1_len;
size_t s2_len;
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
if (!s1 || !s2)
return (NULL);
str = (char *)malloc(sizeof(char) * (s1_len + s2_len + 1));
if (!str)
return (NULL);
ft_strlcpy(str, s1, s1_len + 1);
ft_strlcpy(str + s1_len, s2, s2_len + 1);
return (str);
}
/*#include <stdio.h>
int main()
{
const char s1[] = "ppp";
const char s2[] = "sss";
printf("%s", ft_strjoin(s1, s2));
return 0;
}
*/
s1의 왼쪽에서 set이 아닌 문자가 나올 때'부터 's1의 오른쪽에서 set이 아닌 문자가 나올 때'까지 자르는 함수이다.
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
size_t i;
if (!s1)
return (0);
i = 0;
start = 0;
end = ft_strlen(s1) - 1;
while (s1[i] && ft_strchr(set, s1[i]))
i++;
start = i;
if (start >= ft_strlen(s1))
return ((char *)ft_calloc(sizeof(char), 1)); //return(ft_strdup(""));
i = ft_strlen(s1) - 1;
while (i && s1[i] && ft_strchr(set, s1[i]))
i--;
end = i;
return (ft_substr(s1, start, (end - start + 1)));
}
/*
#include <stdio.h>
int main(void)
{
char s1[] = "abc";
char set[] = "";
char *str;
str = ft_strtrim(s1, set);
printf("%s", str);
}*/
할당 실패시 NULL을 리턴하고, 성공시 자른 문자열을 리턴
문장을 구분자를 기준으로 스플릿 해주는 함수
#include "libft.h"
static int detec_sep(char ori, char c)
{
if (ori == c)
return (1);
return (0);
}
static int word_count(char const *s, char c)
{
int i;
int count;
count = 0;
i = 0;
while (s[i])
{
while (detec_sep(s[i], c) && s[i])
i++;
if (!detec_sep(s[i], c) && s[i])
{
count++;
while (!detec_sep(s[i], c) && s[i])
i++;
}
}
return (count);
}
static char *word_cut(char **words, char const *s, char c)
{
int i;
int j;
char *des;
j = 0;
i = 0;
while (!detec_sep(s[i], c) && s[i] != '\0')
i++;
des = (char *)malloc((i + 1) * sizeof(char));
if (!des)
{
free(words);
return (0);
}
while (j < i)
{
des[j] = s[j];
j++;
}
des[j] = '\0';
return (des);
}
char **ft_split(char const *s, char c)
{
int word_num;
char **words;
int i;
int j;
j = 0;
i = 0;
word_num = word_count(s, c);
words = (char **)malloc(sizeof(char *) * (word_num + 1));
if (!words)
return (NULL);
while (s[i])
{
while (s[i] && detec_sep(s[i], c))
i++;
if (s[i] != '\0')
words[j++] = word_cut(words, &s[i], c);
while (!detec_sep(s[i], c) && s[i])
i++;
}
words[j] = 0;
return (words);
}
/*#include <stdio.h>
int main()
{
char const *s = "aaaaa aaaaa";
char *c;
char **word;
c = " ";
word = ft_split(s, c[0]);
for (int i = 0; i < 2; i++)
{
printf("%s\n", word[i]);
}
}*/
1차원 배열 할당 실패시 더블 포인터로 할당해준 배열을 프리해줘야 함.
→ 왜 해줘야 할까..
fd란 프로세스가 파일을 다룰때 사용하는 개념으로 프로세스에서 특정 파일에 접근할 때 사용하는 추상적인 값이다.
일반적으로 0이 아닌 정수 값을 갖는다.
0 = 표준 입력
1 = 표준 출력
2 = 표준 에러
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
int i;
i = 0;
while (s[i] != '\0')
{
write(fd, &s[i], 1);
i++;
}
write(fd, "\n", 1);
}
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
int i;
i = 0;
while (s[i] != '\0')
{
write(fd, &s[i], 1);
i++;
}
}
문자열 's' 의 각 문자를 순회하며 함수 'f'를 적용하고, 해당 문자의 인덱스를 함수 'f'의 첫 번째 인자로 사용합니다. 또한 각 문자의 주소값이 'f' 함수의 두 번째 인자로 사용되며, 경우에 따라 수정될 수 있습니다.
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
i = 0;
while (s[i])
{
f(i, &s[i]);
i++;
}
}
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t s1_len;
size_t s2_len;
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
if (!s1 || !s2)
return (NULL);
str = (char *)malloc(sizeof(char) * (s1_len + s2_len + 1));
if (!str)
return (NULL);
ft_strlcpy(str, s1, s1_len + 1);
ft_strlcpy(str + s1_len, s2, s2_len + 1);
return (str);
}
/*#include <stdio.h>
int main()
{
const char s1[] = "ppp";
const char s2[] = "sss";
printf("%s", ft_strjoin(s1, s2));
return 0;
}
*/
리스트 'lst'의 요소들을 순회하며 각 요소의 content에 함수 'f'를 연속적으로 적용시킵니다. 또한 함수 'f'를 적용시킨 결과물들을 content로 담은 새로운 리스트를 생성합니다. 'del' 함수들은 필요 시 각 요소의 content를 삭제하는 데 사용됩니다.
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *tmp;
t_list *result;
void *new;
result = 0;
while (lst)
{
new = f(lst->content);
tmp = ft_lstnew(new);
if (!tmp)
{
ft_lstclear(&result, del);
del(new);
return (0);
}
ft_lstadd_back(&result, tmp);
lst = lst->next;
}
return (result);
}
malloc(3) 을 통해 메모리를 할당하고 새로운 요소를 반환합니다. 요소 내의 변수 'content' 는 인자로 받아온 'content' 로 초기화되어야 합니다. 요소 내의 변수 'next'는 NULL로 초기화되어야 합니다.
리스트의 맨 마지막에 위치한 요소를 반환합니다.
리스트 'lst' 를 순회하며, 리스트에 포함된 모든 요소들의 content에 함수 'f'를 반복적으로 적용시킵니다.