libft - KimTaebin-ai/study_posts GitHub Wiki
- linux
- makefile
- ๋ฌธ๋ฒ
- linked list
๋ชจ์ ํ ์คํธ ๋๋ฆฌ๋ ๋ฐฉ๋ฒ
bash ~/Libftest/grademe.sh
C์ ํ์ค๋ผ์ด๋ธ๋ฌ๋ฆฌ Libc์ ํจ์๋ค์ ์ด๋ฏธํ ์ด์ ํ์ฌ ๋ง๋ ๋ค.
int ft_isalpha(int c)
{
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
return (1);
else
return (0);
}
์ค๋ช ์๋ต
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
else
return (0);
}
์ค๋ช ์๋ต
int ft_isalnum(int c)
{
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0'
&& c <= '9'))
return (1);
else
return (0);
}
์ค๋ช ์๋ต
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
else
return (0);
}
์ค๋ช ์๋ต
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
else
return (0);
}
์ค๋ช ์๋ต
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
while (s[len] != '\0')
len++;
return (len);
}
๋ฌธ์์ด์ ์ธ์๋ก ๋ฐ์ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ๋ฐํํ๋ค.
ํ๋ผ๋ฏธํฐ ์ธ์๋ก ๋ฐ๋ const char * s
์์ ํค์๋.
์ฆ, ์๋ณธ ๋ฐ์ดํฐ๋ ์ ์งํ๋ ๊ฒ์ด ์ค๊ณ ๋ชฉ์ ์ธ ์ ์ดํดํ๊ณ ๋์ด๊ฐ๋ ๊ฒ์ด ์ข๋ค
void *ft_memset(void *s, int c, size_t n)
{
unsigned char *data;
data = (unsigned char *)s;
while (n--)
{
*data = (unsigned char)c;
data++;
}
return (s);
}
๋ฉ๋ชจ๋ฆฌ์ ๊ฐ์ ํน์ ๊ฐ์ผ๋ก ์ธํ ํ ์ ์๋ ํจ์์ด๋ค.
s
์ n
๋งํผ c
๋ก ์ธํ
ํ๋ค
์ฑ๊ณตํ๋ค๋ฉด ์๋ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๋ฐํํ๊ณ , ์คํจํ๋ค๋ฉด NULL
์ ๋ฐํํ๊ฒ ๋๋ค.
์ธ์๊ฐ์ผ๋ก void
ํ ํฌ์ธํฐ๋ฅผ ๋ฐ๊ณ ์์ผ๋ฏ๋ก unsigned char *
๋ก ๋ณ๊ฒฝ ํ์
char
[00000001]
๋ํ int
ํ ๋ฐฐ์ด์ ๊ฒฝ์ฐ ๋ด๋ถ์์๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ญ์ผ๋ก 4byte์ฉ [00000000][00000000][00000000][00000001] -> 1 ๋ฅผ ๊ธฐ๋ํ๊ฒ ์ง๋ง memset๋ด๋ถ์์ ๋ฉ๋ชจ๋ฆฌ๋ธ๋ญ์ 1byte๋ก ๊ตฌํด๋ฒ๋ ค์ [00000001][00000001][00000001][00000001] -> 16843009 ๊ฐ์ผ๋ก ์ด๊ธฐํ ๋์ด๋ฒ๋ฆฐ๋ค.
void ft_bzero(void *s, size_t n)
{
size_t i;
unsigned char *data;
i = 0;
data = (unsigned char *)s;
while (i < n)
{
data[i] = 0;
i++;
}
}
memset
๊ณผ ๋น์ทํ ๊ธฐ๋ฅ์ ์ํํจ.
๋ค๋ง memset
๊ณผ ๋ค๋ฅด๊ฒ ์ธ์๊ฐ์ผ๋ก int c
๋ฅผ ๋ฐ์ง ์๋๋ค.
0์ผ๋ก ์ด๊ธฐํํด์ค๋ค๋ ์๋ฏธ์ด๋ค.
void *ft_memcpy(void *dest, const void *src, size_t n)
{
unsigned char *_dest;
const unsigned char *_src;
_dest = (unsigned char *)dest;
_src = (const unsigned char *)src;
if (_dest == _src)
return (dest);
while (n--)
{
*_dest = *_src;
_dest++;
_src++;
}
return (dest);
}
memmove
์ ํจ๊ป ๋ฌถ์ฌ์ ๋ง์ด ๋ง์ ํจ์์ด๋ค.
dest
์ src
๋ก๋ถํฐ n
๋งํผ copyํด์ค๋ค.
๋ค๋ง dest
์ src
๊ฐ ๊ฒน์น๋ฉด ๋์์ด ๋์ง ์๋๋ค.
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t i;
unsigned char *_dest;
const unsigned char *_src = (const unsigned char *)src;
i = 0;
_dest = (unsigned char *)dest;
if (dest > src)
{
while (n--)
_dest[n] = _src[n];
}
if (dest < src)
{
while (n--)
{
_dest[i] = _src[i];
i++;
}
}
return (dest);
}
dest
์ src
๋ก๋ถํฐ n
๋งํผ copyํด์ค๋ค.
๊ทธ๋ฌ๋ memcpy
์ ๋ค๋ฅด๊ฒ ๋ ๋ฌธ์์ด์ด ๊ฒน์น ์ ์๊ณ , ๋ณต์ฌ๋ ๋ด์ฉ๋ ํผ์๋์ง ์์์ผ ํ๋ค.
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (src[j])
j++;
if (size == 0)
return (j);
while (src[i] && i < size - 1)
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
return (j);
}
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t i;
size_t dest_l;
size_t src_l;
i = 0;
dest_l = ft_strlen(dst);
src_l = ft_strlen(src);
if (size <= dest_l)
return (size + src_l);
while (src[i] && dest_l + i < size - 1)
{
dst[dest_l + i] = src[i];
i++;
}
dst[dest_l + i] = '\0';
return (src_l + dest_l);
}
int ft_toupper(int c)
{
int diff;
diff = 'A' - 'a';
if (c >= 'a' && c <= 'z')
c += diff;
return (c);
}
int ft_tolower(int c)
{
int diff;
diff = 'A' - 'a';
if (c >= 'A' && c <= 'Z')
c -= diff;
return (c);
}
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*s == (unsigned char)c)
return ((char *)s);
s++;
}
if (*s == (unsigned char)c)
return ((char *)s);
return (0);
}
char *ft_strrchr(const char *s, int c)
{
int i;
i = 0;
while (*s)
{
s++;
i++;
}
while (i >= 0)
{
if (*s == (unsigned char)c)
return ((char *)s);
s--;
i--;
}
return (0);
}
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
unsigned char *_s1;
unsigned char *_s2;
i = 0;
_s1 = (unsigned char *)s1;
_s2 = (unsigned char *)s2;
while (*_s1 && *_s2 && i < n)
{
if (*_s1 != *_s2)
return (*_s1 - *_s2);
_s1++;
_s2++;
i++;
}
if (i != n)
return (*_s1 - *_s2);
return (0);
}
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char *dst;
dst = (unsigned char *)s;
while (n--)
{
if (*dst == (unsigned char)c)
return (dst);
dst++;
}
return (0);
}
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *pt1 = (unsigned char *)s1;
const unsigned char *pt2 = (unsigned char *)s2;
while (n--)
{
if (*pt1 != *pt2)
return (*pt1 - *pt2);
pt1++;
pt2++;
}
return (0);
}
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (*little == '\0')
return ((char *)big);
while (big[i] && i < len)
{
j = 0;
while (big[i + j] == little[j] && i + j < len)
{
if (ft_strlen(little) - j <= 1)
return ((char *)big + i);
j++;
}
i++;
}
return (0);
}
int ft_atoi(const char *nptr)
{
int sign;
int i;
unsigned long long int num;
num = 0;
sign = 1;
i = 0;
while (nptr[i] == ' ' || (nptr[i] >= 9 && nptr[i] <= 13))
i++;
if (nptr[i] == '+' && (nptr[i + 1] >= '0' && nptr[i + 1] <= '9'))
i++;
if (nptr[i] == '-' && (nptr[i + 1] >= '0' && nptr[i + 1] <= '9'))
{
sign *= -1;
i++;
}
while (nptr[i] >= '0' && nptr[i] <= '9')
{
num = num * 10 + (nptr[i] - '0');
i++;
}
return (sign * num);
}
๋ค์ด์จ ๋ฌธ์์ด์ ์ซ์๋ก ๋ฐํํ๋ ํจ์
- ๋ฌธ์์ด์ ์ฒ์ ๊ณต๋ฐฑ ๋ถ๋ถ์ ์ ๋ถ ๋ฌด์ํ๋ค.
- ๋ฌธ์์ด์ ์ฒ์ +,-๊ธฐํธ๋ฅผ ์ธ์ํ๋ค.
- ์์ ๊ท์น๋ค์ ํฌํจํ์ฌ ์ซ์๋ง์ ์ธ์ํ์ฌ int์ผ๋ก ๋ณํํ๋ค.
libc ์์์ atoi ํจ์๋ LONG_MAX
๊ณผ LONG_MIN
์ ๋ฒ์๋ฅผ ๋์ด๊ฐ์ ๋ 0๊ณผ -1์ ๋ฆฌํดํ์ง๋ง, libft์์๋ ์ค๋ฒ/์ธ๋ํ๋ก์ฐ ์ฒ๋ฆฌ๊ฐ๊น์ง๋ ์ ๊ฒฝ์ฐ์ง ์์ผ๋ฏ๋ก ๋ฌด์ํด๋ ๊ด์ฐฎ๋ค.
ft_atoi ์ฝ์งํ ์ ๋ฐฐ๋ ์๋ฃ
#include <limits.h>
void *ft_calloc(size_t nmemb, size_t size)
{
size_t total_size;
void *ptr;
if (nmemb != 0 && size > UINT_MAX / nmemb)
return (NULL);
total_size = nmemb * size;
ptr = malloc(total_size);
if (ptr == NULL)
{
return (NULL);
}
ft_memset(ptr, 0, total_size);
return (ptr);
}
char *ft_strdup(const char *s)
{
char *result;
size_t i;
i = 0;
result = (char *)malloc(sizeof(char) * ft_strlen(s) + 1);
if (result == NULL)
return (0);
while (i < ft_strlen(s))
{
result[i] = s[i];
i++;
}
result[i] = '\0';
return (result);
}
libc์ ํฌํจ๋์ด ์์ง ์๊ฑฐ๋ ๋ค๋ฅธ ํ์์ผ๋ก ํฌํจ๋ ํจ์๋ค์ ์ฌ๊ตฌํํ๋ค.
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t size;
char *sc;
i = 0;
if (s == 0)
return (0);
size = ft_strlen(s);
if (size < start)
len = 0;
else if (len > size - start)
len = size - start;
sc = (char *)malloc(sizeof(char) * (len + 1));
if (sc == NULL)
return (0);
while (len--)
{
sc[i] = s[start];
start++;
i++;
}
sc[i] = '\0';
return (sc);
}
int main(void)
{
char *test1;
char *result;
// ํ
์คํธ ์ผ์ด์ค๋ค
test1 = "Hello, World!";
// Test case 1: ์ ์์ ์ผ๋ก ๋ถ๋ถ ๋ฌธ์์ด ์ถ์ถ (start = 7, len = 5)
result = ft_substr(test1, 7, 5);
if (result != NULL)
{
printf("Test 1 (start=7, len=5): %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "World"
free(result);
}
// Test case 2: ์์ ์์น๊ฐ ์ ์ฒด ๊ธธ์ด๋ณด๋ค ํฐ ๊ฒฝ์ฐ (start = 20)
result = ft_substr(test1, 20, 5);
if (result != NULL)
{
printf("Test 2 (start=20, len=5): %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: ""
free(result);
}
// Test case 3: ๋ถ๋ถ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ๋ฌธ์์ด ๋์ ๋๋ ๊ฒฝ์ฐ (start = 7, len = 10)
result = ft_substr(test1, 7, 10);
if (result != NULL)
{
printf("Test 3 (start=7, len=10): %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "World!"
free(result);
}
// Test case 4: ์์ ์์น๊ฐ 0์ธ ๊ฒฝ์ฐ (start = 0, len = 5)
result = ft_substr(test1, 0, 5);
if (result != NULL)
{
printf("Test 4 (start=0, len=5): %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "Hello"
free(result);
}
// Test case 5: ๊ธธ์ด๊ฐ 0์ธ ๊ฒฝ์ฐ (start = 5, len = 0)
result = ft_substr(test1, 5, 0);
if (result != NULL)
{
printf("Test 5 (start=5, len=0): %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: ""
free(result);
}
return (0);
}
substr๋ s์ ๋ฌธ์์ด์์ start์ ๊ธธ์ด์์ len๊ธธ์ด๋งํผ ๋ณต์ฌํ์ฌ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํํ๋ ํจ์์ด๋ค.
- s์ null์ฒ๋ฆฌ
- s์ ๊ธธ์ด๊ฐ start์ ๊ธธ์ด๋ณด๋ค ์์ ๊ฒฝ์ฐ ๋น ๋ฌธ์์ด ๋ฐํ
- ๋ฌธ์์ด์ด๊ธฐ ๋๋ฌธ์ โ\0โ๋ฌธ์ ์ฒ๋ฆฌ +1 ์ฒ๋ฆฌํด์ผํจ
char *ft_strjoin(char const *s1, char const *s2)
{
size_t i;
size_t j;
char *sc;
i = 0;
j = 0;
sc = (char *)ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (sc == NULL)
return (0);
while (s1[i])
{
sc[i] = s1[i];
i++;
}
while (s2[j])
{
sc[i + j] = s2[j];
j++;
}
return (sc);
}
int main(void)
{
char *result;
// ํ
์คํธ ์ผ์ด์ค๋ค
// Test case 1: ๋ ๋ฌธ์์ด์ ์ ์์ ์ผ๋ก ๊ฒฐํฉ
result = ft_strjoin("Hello, ", "World!");
if (result != NULL)
{
printf("Test 1: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "Hello, World!"
free(result);
}
// Test case 2: ์ฒซ ๋ฒ์งธ ๋ฌธ์์ด์ด ๋น ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ
result = ft_strjoin("", "World!");
if (result != NULL)
{
printf("Test 2: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "World!"
free(result);
}
// Test case 3: ๋ ๋ฒ์งธ ๋ฌธ์์ด์ด ๋น ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ
result = ft_strjoin("Hello, ", "");
if (result != NULL)
{
printf("Test 3: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "Hello, "
free(result);
}
// Test case 4: ๋ ๋ฌธ์์ด ๋ชจ๋ ๋น ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ
result = ft_strjoin("", "");
if (result != NULL)
{
printf("Test 4: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: ""
free(result);
}
// Test case 5: ํฐ ๋ฌธ์์ด ๊ฒฐํฉ
result = ft_strjoin("This is a long string ",
"and this is another long string.");
if (result != NULL)
{
printf("Test 5: %s\n", result);
// ๊ธฐ๋ ๊ฒฐ๊ณผ: "This is a long string and this is another long string."
free(result);
}
return (0);
}
s1๋ค์ s2๋ฅผ ๋ถ์ธ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํํ๋ค.
- s1๊ณผ s2์ ๊ธธ์ด์ฒดํฌ ๋ฐ ๋๋ฌธ์ ์ฝ์
int is_check(char c, char const *set)
{
int i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int start;
int end;
char *sc;
i = 0;
start = 0;
end = ft_strlen(s1);
while (s1[start] && is_check(s1[start], set))
start++;
while (end > start && is_check(s1[end - 1], set))
end--;
sc = (char *)malloc(sizeof(char) * (end - start + 1));
if (sc == NULL)
return (0);
while (end > start)
{
sc[i] = s1[start];
i++;
start++;
}
sc[i] = '\0';
return (sc);
}
int main(void)
{
char *result;
// Test case 1: ์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ
result = ft_strtrim(" Hello, World! ", " ");
if (result != NULL)
{
printf("Test 1: '%s'\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: 'Hello, World!'
free(result);
}
// Test case 2: ์๋ค ํน์ ๋ฌธ์ ์ ๊ฑฐ
result = ft_strtrim("--Hello--", "-");
if (result != NULL)
{
printf("Test 2: '%s'\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: 'Hello'
free(result);
}
// Test case 3: ๋ฌธ์ ์งํฉ์ ํฌํจ๋์ง ์๋ ๊ฒฝ์ฐ (๋ณํ ์์)
result = ft_strtrim("Hello", "-");
if (result != NULL)
{
printf("Test 3: '%s'\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: 'Hello'
free(result);
}
// Test case 4: ๋ฌธ์์ด์ด ๋ชจ๋ ๋ฌธ์ ์งํฉ์ผ๋ก๋ง ๊ตฌ์ฑ๋ ๊ฒฝ์ฐ
result = ft_strtrim("-----", "-");
if (result != NULL)
{
printf("Test 4: '%s'\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: ''
free(result);
}
// Test case 5: ๋น ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ
result = ft_strtrim("", "-");
if (result != NULL)
{
printf("Test 5: '%s'\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: ''
free(result);
}
// Test case 6: ๋ฌธ์ ์งํฉ์ด ๋น ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ (๋ณํ ์์)
result = ft_strtrim("Hello", "");
if (result != NULL)
{
printf("Test 6: '%s'\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: 'Hello'
free(result);
}
return (0);
}
s1๋ฌธ์์ด์ ์๋์์ set๋ฌธ์์ด์ ์ ๊ฑฐํ๋ ํจ์์ด๋ค.
ft_strchr(3)์ ์ฌ์ฉํ์ฌ set์ ๊ฒ์ฌํ์ฌ ํ์ดํ๋ค.
int ft_wordlen(char const *s, char c)
{
int i;
int in_word;
i = 0;
in_word = 0;
while (*s)
{
if (*s != c && in_word == 0)
{
in_word = 1;
i++;
}
else if (*s == c)
in_word = 0;
s++;
}
return (i);
}
int ft_cpy(char **sp, char const *start, char const *s, int j)
{
int len;
int k;
len = s - start;
k = 0;
sp[j] = (char *)malloc(sizeof(char) * (len + 1));
if (sp[j] == NULL)
{
while (j-- > 0)
free(sp[j]);
free(sp);
return (0);
}
while (k < len)
{
sp[j][k] = start[k];
k++;
}
sp[j][k] = '\0';
return (1);
}
char **nom(char **sp, char const *s, char c)
{
char const *start;
int j;
j = 0;
while (*s)
{
while (*s && (*s == c))
s++;
start = s;
if (*s && (*s != c))
{
while (*s && (*s != c))
s++;
if (ft_cpy(sp, start, s, j) == 0)
return (0);
j++;
}
}
sp[j] = 0;
return (sp);
}
char **ft_split(char const *s, char c)
{
char **sc;
if (s == 0)
return (0);
sc = (char **)malloc(sizeof(char *) * (ft_wordlen(s, c) + 1));
if (sc == NULL)
return (0);
return (nom(sc, s, c));
}
int main(void)
{
char **result;
// Test case 1: ๋ฌธ์์ด์ ๊ณต๋ฐฑ์ผ๋ก ๋๋
result = ft_split("Hello world this is a test", ' ');
if (result != NULL)
{
printf("Test 1: ");
for (int i = 0; result[i] != NULL; i++)
{
printf("'%s' ", result[i]);
}
printf("\n");
free_split(result);
}
// Test case 2: ๋ฌธ์์ด์ ์ผํ๋ก ๋๋
result = ft_split("one,two,three,four", ',');
if (result != NULL)
{
printf("Test 2: ");
for (int i = 0; result[i] != NULL; i++)
{
printf("'%s' ", result[i]);
}
printf("\n");
free_split(result);
}
// Test case 3: ๋ฌธ์์ด์ ๊ตฌ๋ถ์๊ฐ ์์ ๋
result = ft_split("HelloWorld", ' ');
if (result != NULL)
{
printf("Test 3: ");
for (int i = 0; result[i] != NULL; i++)
{
printf("'%s' ", result[i]);
}
printf("\n");
free_split(result);
}
// Test case 4: ๋ฌธ์์ด์ด ๋ชจ๋ ๊ตฌ๋ถ์๋ก๋ง ๊ตฌ์ฑ๋ ๊ฒฝ์ฐ
result = ft_split(",,,,,", ',');
if (result != NULL)
{
printf("Test 4: ");
for (int i = 0; result[i] != NULL; i++)
{
printf("'%s' ", result[i]);
}
printf("\n");
free_split(result);
}
// Test case 5: ๋น ๋ฌธ์์ด์ ์
๋ ฅํ ๊ฒฝ์ฐ
result = ft_split("", ',');
if (result != NULL)
{
printf("Test 5: ");
for (int i = 0; result[i] != NULL; i++)
{
printf("'%s' ", result[i]);
}
printf("\n");
free_split(result);
}
return (0);
}
splitํจ์๋ ๋ฌธ์์ด ํ๋์ ๊ตฌ๋ถ์๋ฅผ ์ ๋ ฅ๋ฐ์์ ๋ฌธ์์ด์ ๊ตฌ๋ถ์๋ก ๋ถ๋ฆฌํ์ฌ 2์ฐจ์ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋ฐํํ๋ ํจ์์ด๋ค.
- ๋ฌธ์์ด์ ์กด์ฌํ๋ ๋จ์ด(set์ผ๋ก ๊ตฌ๋ถ๋๋ค.)์ ๊ธธ์ด๋ฅผ ํ์ ํ๋ค.
- ์์ ๊ณต๋ถํ split๊ณผ ๋ค๋ฅด๊ฒ ํ์ดํธ ์คํ์ด์ค๋ฅผ ๋ฐ์ง์ง ์๊ณ ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์๊ฐํ๋ค.
- ๋์ ํ ๋น์ด ์ ๋๋ก ์ด๋ฃจ์ด์ ธ์ผ ํ๊ธฐ ๋๋ฌธ์ ํฌ๊ธฐ์ ๋ง๊ฒ ๋์ ํ ๋นํ๋ค.
๋ด๋ถ ํจ์๊ฐ ๋ง์ด ํ์ํ๋ static์ ์ธํ๊ฑฐ๋ ํจ์๋ฅผ ์ถ๊ฐํ์ฌ ์ฌ์ฉํ ๊ฒ
int ft_ilen(int n)
{
int i;
i = 0;
if (n == 0)
i = 1;
else if (n < 0)
{
i = 1;
while (n)
{
n = n / 10;
i++;
}
}
else if (n > 0)
{
while (n)
{
n = n / 10;
i++;
}
}
return (i);
}
char *ft_itoa(int n)
{
int size_n;
unsigned int fn;
char *ia;
size_n = ft_ilen(n);
ia = (char *)malloc(sizeof(char) * size_n + 1);
if (ia == NULL)
return (0);
ia[size_n] = '\0';
if (n < 0)
{
ia[0] = '-';
fn = n * (-1);
}
else
fn = n;
while (fn)
{
ia[size_n - 1] = (fn % 10) + '0';
fn = fn / 10;
size_n--;
}
if (n == 0)
ia[0] = '0';
return (ia);
}
int main(void)
{
char *result;
// Test case 1: ์์
result = ft_itoa(12345);
if (result != NULL)
{
printf("Test 1: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "12345"
free(result);
}
// Test case 2: ์์
result = ft_itoa(-12345);
if (result != NULL)
{
printf("Test 2: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "-12345"
free(result);
}
// Test case 3: 0์ธ ๊ฒฝ์ฐ
result = ft_itoa(0);
if (result != NULL)
{
printf("Test 3: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "0"
free(result);
}
// Test case 4: ํฐ ์์
result = ft_itoa(2147483647);
if (result != NULL)
{
printf("Test 4: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "2147483647"
free(result);
}
// Test case 5: ํฐ ์์
result = ft_itoa(-2147483648);
if (result != NULL)
{
printf("Test 5: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "-2147483648"
free(result);
}
return (0);
}
itoaํจ์๋ ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ๋ฐํํด์ฃผ๋ ํจ์์ด๋ค.
- 0์ ๋ํ ์์ธ ์ฒ๋ฆฌ์ ์ซ์๊ธธ์ด๋ฅผ ๊ตฌํ๋ ํจ์๊ฐ ํ์ํจ
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
size_t i;
char *sc;
if (s == 0)
return (0);
i = 0;
sc = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1));
if (sc == NULL)
return (0);
while (i < ft_strlen(s))
{
sc[i] = f(i, s[i]);
i++;
}
sc[i] = '\0';
return (sc);
}
// ์์ ํจ์๋ค
char to_upper(unsigned int index, char c)
{
// ๋ชจ๋ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ
if (c >= 'a' && c <= 'z')
{
return (c - 32);
}
return (c);
}
char add_index(unsigned int index, char c)
{
// ๊ฐ ๋ฌธ์์ ์ธ๋ฑ์ค ๊ฐ์ ๋ํ ๋ฌธ์๋ก ๋ณํ
return (c + index);
}
char toggle_case(unsigned int index, char c)
{
// ์ธ๋ฑ์ค๊ฐ ์ง์๋ฉด ๋๋ฌธ์๋ก, ํ์๋ฉด ์๋ฌธ์๋ก ๋ณํ
if (index % 2 == 0)
{
if (c >= 'a' && c <= 'z')
{
return (c - 32);
}
}
else
{
if (c >= 'A' && c <= 'Z')
{
return (c + 32);
}
}
return (c);
}
// ํ
์คํธ ์ฝ๋
int main(void)
{
char *result;
// Test case 1: ๋ชจ๋ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ
result = ft_strmapi("hello world", to_upper);
if (result != NULL)
{
printf("Test 1: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "HELLO WORLD"
free(result);
}
// Test case 2: ๊ฐ ๋ฌธ์์ ์ธ๋ฑ์ค ๊ฐ์ ๋ํ ๋ฌธ์๋ก ๋ณํ
result = ft_strmapi("abcde", add_index);
if (result != NULL)
{
printf("Test 2: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "acegi"
free(result);
}
// Test case 3: ์ธ๋ฑ์ค๊ฐ ์ง์๋ฉด ๋๋ฌธ์, ํ์๋ฉด ์๋ฌธ์๋ก ๋ณํ
result = ft_strmapi("HeLLo WoRLd", toggle_case);
if (result != NULL)
{
printf("Test 3: %s\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "HELlO WoRLd"
free(result);
}
// Test case 4: ๋น ๋ฌธ์์ด
result = ft_strmapi("", to_upper);
if (result != NULL)
{
printf("Test 4: '%s'\n", result); // ๊ธฐ๋ ๊ฒฐ๊ณผ: ''
free(result);
}
// Test case 5: NULL ์
๋ ฅ
result = ft_strmapi(NULL, to_upper);
if (result == NULL)
{
printf("Test 5: NULL result (expected)\n"); // ๊ธฐ๋ ๊ฒฐ๊ณผ: NULL ์ถ๋ ฅ
}
return (0);
}
์๋ณธ ๋ฌธ์์ด์์ ํจ์f๋ฅผ ์ ์ฉํ์ฌ ์์ฑํ ๋ฌธ์์ด ๋ฐํํ๋ ํจ์
- ๋ฌธ์์ด s๋ฅผ ์ํํ๋ฉฐ ํจ์โfโ๋ฅผ ์ ์ฉํ๊ณ ์๋ก์ด ๋ฌธ์์ด์ ์์ฑํ๋ค.
- null์ฒ๋ฆฌ ์ค์
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
size_t i;
size_t len;
if (s == 0)
return ;
len = ft_strlen(s);
i = 0;
while (i < len)
{
f(i, &s[i]);
i++;
}
}
// ์์ ํจ์๋ค
void to_upper(unsigned int index, char *c)
{
// ๊ฐ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ
if (*c >= 'a' && *c <= 'z')
{
*c = *c - 32;
}
}
void add_index(unsigned int index, char *c)
{
// ๊ฐ ๋ฌธ์์ ์ธ๋ฑ์ค ๊ฐ์ ๋ํจ
*c = *c + index;
}
void toggle_case(unsigned int index, char *c)
{
// ์ธ๋ฑ์ค๊ฐ ์ง์๋ฉด ๋๋ฌธ์, ํ์๋ฉด ์๋ฌธ์๋ก ๋ณํ
if (index % 2 == 0 && *c >= 'a' && *c <= 'z')
{
*c = *c - 32; // ๋๋ฌธ์๋ก ๋ณํ
}
else if (index % 2 != 0 && *c >= 'A' && *c <= 'Z')
{
*c = *c + 32; // ์๋ฌธ์๋ก ๋ณํ
}
}
// ํ
์คํธ ์ฝ๋
int main(void)
{
char str1[] = "hello world";
char str2[] = "abcde";
char str3[] = "HeLLo WoRLd";
// Test case 1: ๋ชจ๋ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ
ft_striteri(str1, to_upper);
printf("Test 1: %s\n", str1); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "HELLO WORLD"
// Test case 2: ๊ฐ ๋ฌธ์์ ์ธ๋ฑ์ค ๊ฐ์ ๋ํจ
ft_striteri(str2, add_index);
printf("Test 2: %s\n", str2); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "acegi"
// Test case 3: ์ธ๋ฑ์ค๊ฐ ์ง์๋ฉด ๋๋ฌธ์, ํ์๋ฉด ์๋ฌธ์๋ก ๋ณํ
ft_striteri(str3, toggle_case);
printf("Test 3: %s\n", str3); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "HELlO WoRLd"
return (0);
}
striteriํจ์๋ ๋ฌธ์์ด์ ์ํํ๋ฉฐ ํจ์ f๋ฅผ ์ ์ฉ์ฌํค๋ ํจ์์ด๋ค.
- ์ํํ๋ฉด์ ์ ์ฉ์ํค๋๋ฐ ๊ฐ๋ณ ํจ์ f์ธ์๊ฐ์ด (char *)์ธ ์ด์ ๋ ๋ฐํ๊ฐ์ด void์ด๋ฉฐ ์ฃผ์๊ฐ์ ์ ๋ฌํ์ฌ ํจ์์์ ์ง์ ํจ์๋ฅผ ์ ์ฉํ ์ ์๋๋ก ํฌ์ธํฐ๋ก ์ฃผ์๋ฅผ ๋๊ฒจ์ค๋ค.
fd 0 ํ์ค ์ ๋ ฅ
1 ํ์ค ์ถ๋ ฅ
2 ํ์ค ์๋ฌ
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
putchar_fdํจ์๋ ํด๋นํ๋ fd(ํ์ผ๋์คํฌ๋ฆฝํฐ ๊ฐ)์ผ๋ก ์ด๋ ค์๋ ์คํธ๋ฆผ์ ๋ฐ์ดํธ์ด์ c๋ฌธ์๋ฅผ ํ๋ ค๋ณด๋ด๋ ํจ์์ด๋ค.
void ft_putstr_fd(char *s, int fd)
{
if (s == 0)
return ;
write(fd, s, ft_strlen(s));
}
putstr_fdํจ์๋ ํด๋นํ๋ fd(ํ์ผ๋์คํฌ๋ฆฝํฐ ๊ฐ)์ผ๋ก ์ด๋ ค์๋ ์คํธ๋ฆผ์ ๋ฐ์ดํธ์ด์ ๋ฌธ์์ด s๋ฅผ ํ๋ ค๋ณด๋ด๋ ํจ์์ด๋ค.
void ft_putendl_fd(char *s, int fd)
{
if (s == 0)
return ;
write(fd, s, ft_strlen(s));
write(fd, "\n", 1);
}
putendl_fdํจ์๋ putstr_fdํจ์์์ ์ค๋ฐ๊ฟ(โ\nโ)์ ์ถ๊ฐํ ํจ์์ด๋ค.
void ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
ft_putstr_fd("-2147483648", fd);
else if (n >= 0 && n <= 9)
ft_putchar_fd(n + '0', fd);
else if (n > 0)
{
ft_putnbr_fd(n / 10, fd);
ft_putchar_fd(n % 10 + '0', fd);
}
else if (n < 0)
{
ft_putchar_fd('-', fd);
ft_putnbr_fd(-n, fd);
}
}
putnbr_fdํจ์๋ ๋๊ฐ์ ๋ฐฉ์์ผ๋ก ์ซ์๋ก ๋ฐ๊พธ์ ์คํธ๋ฆผ์ ํ๋ ค๋ณด๋ธ๋ค.
t_list *ft_lstnew(void *content)
{
t_list *newnode;
newnode = (t_list *)malloc(sizeof(t_list));
if (newnode == NULL)
return (NULL);
newnode->content = content;
newnode->next = NULL;
return (newnode);
}
int main(void)
{
t_list *node1;
t_list *node2;
t_list *node3;
int value1;
char *str;
// Test case 1: ์ ์๋ฅผ content๋ก ๊ฐ์ง๋ ๋
ธ๋ ์์ฑ
value1 = 42;
node1 = ft_lstnew(&value1);
if (node1 != NULL)
{
printf("Test 1: Node content (int) = %d\n", *(int *)node1->content);
// ๊ธฐ๋ ๊ฒฐ๊ณผ: 42
free(node1);
}
else
{
printf("Test 1: Node creation failed.\n");
}
// Test case 2: ๋ฌธ์์ด์ content๋ก ๊ฐ์ง๋ ๋
ธ๋ ์์ฑ
str = "Hello, world!";
node2 = ft_lstnew(str);
if (node2 != NULL)
{
printf("Test 2: Node content (string) = %s\n", (char *)node2->content);
// ๊ธฐ๋ ๊ฒฐ๊ณผ: "Hello, world!"
free(node2);
}
else
{
printf("Test 2: Node creation failed.\n");
}
// Test case 3: NULL content๋ฅผ ๊ฐ์ง๋ ๋
ธ๋ ์์ฑ
node3 = ft_lstnew(NULL);
if (node3 != NULL)
{
printf("Test 3: Node content (NULL) = %s\n",
(node3->content == NULL ? "NULL" : "Not NULL"));
// ๊ธฐ๋ ๊ฒฐ๊ณผ: "NULL"
free(node3);
}
else
{
printf("Test 3: Node creation failed.\n");
}
return (0);
}
๋ฆฌ์คํธํ๋๋ฅผ ๋์ ํ ๋นํ๊ณ content๋ฅผ ๋ฃ์ด์ค๋ค next๋ฅผ null์ ๊ฐ๋ฆฌํค๊ฒ ํ๋ ํจ์์ด๋ค.
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (lst == 0 || new == 0)
return ;
new->next = *lst;
*lst = new;
}
// ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ถ๋ ฅํ๋ ํจ์
void print_list(t_list *lst)
{
t_list *current;
current = lst;
while (current != NULL)
{
printf("%s -> ", (char *)current->content);
current = current->next;
}
printf("NULL\n");
}
// ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ํด์ ํ๋ ํจ์
void free_list(t_list *lst)
{
t_list *current;
t_list *next;
current = lst;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
}
// ํ
์คํธ ์ฝ๋
int main(void)
{
t_list *new_node;
t_list *head = NULL; // ์ด๊ธฐ ๋ฆฌ์คํธ๋ ๋น์ด ์์
// Test case 1: ๋น ๋ฆฌ์คํธ์ ์ ๋
ธ๋ ์ถ๊ฐ
new_node = ft_lstnew("First Node");
ft_lstadd_front(&head, new_node);
printf("Test 1: ");
print_list(head); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "First Node -> NULL"
// Test case 2: ๊ธฐ์กด ๋ฆฌ์คํธ ์์ ์ ๋
ธ๋ ์ถ๊ฐ
new_node = ft_lstnew("New Front Node");
ft_lstadd_front(&head, new_node);
printf("Test 2: ");
print_list(head); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "New Front Node -> First Node -> NULL"
// Test case 3: ๋ ๋ค๋ฅธ ๋
ธ๋ ์ถ๊ฐ
new_node = ft_lstnew("Another Front Node");
ft_lstadd_front(&head, new_node);
printf("Test 3: ");
print_list(head);
// ๊ธฐ๋ ๊ฒฐ๊ณผ: "Another Front Node -> New Front Node -> First Node -> NULL"
// ๋ฉ๋ชจ๋ฆฌ ํด์
free_list(head);
return (0);
}
๋ฆฌ์คํธ head๋ถ๋ถ์ new๋ฅผ ์ถ๊ฐํ๋ ํจ์์ด๋ค.
- null ์ฒดํฌ ํ head๋ถ๋ถ์ด๊ธฐ ๋๋ฌธ์ *lst๋ก ์ ๊ทผํ์ฌ ์ฐ๊ฒฐํ๋ค.
int ft_lstsize(t_list *lst)
{
t_list *current;
int i;
current = lst;
i = 0;
while (current != NULL)
{
i++;
current = current->next;
}
return (i);
}
// ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ํด์ ํ๋ ํจ์
void free_list(t_list *lst)
{
t_list *current;
t_list *next;
current = lst;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
}
// ํ
์คํธ ์ฝ๋
int main(void)
{
t_list *head;
int size;
head = NULL;
// Test case 1: ๋น ๋ฆฌ์คํธ์ ํฌ๊ธฐ
size = ft_lstsize(head);
printf("Test 1: List size = %d (Expected: 0)\n", size);
// Test case 2: ๋
ธ๋ 1๊ฐ ์ถ๊ฐํ ๋ฆฌ์คํธ์ ํฌ๊ธฐ
ft_lstadd_front(&head, ft_lstnew("First Node"));
size = ft_lstsize(head);
printf("Test 2: List size = %d (Expected: 1)\n", size);
// Test case 3: ๋
ธ๋ 2๊ฐ ์ถ๊ฐํ ๋ฆฌ์คํธ์ ํฌ๊ธฐ
ft_lstadd_front(&head, ft_lstnew("Second Node"));
size = ft_lstsize(head);
printf("Test 3: List size = %d (Expected: 2)\n", size);
// Test case 4: ๋
ธ๋ 3๊ฐ ์ถ๊ฐํ ๋ฆฌ์คํธ์ ํฌ๊ธฐ
ft_lstadd_front(&head, ft_lstnew("Third Node"));
size = ft_lstsize(head);
printf("Test 4: List size = %d (Expected: 3)\n", size);
// ๋ฉ๋ชจ๋ฆฌ ํด์
free_list(head);
return (0);
}
๋ฆฌ์คํธ์ ์ฌ์ด์ฆ๋ฅผ ๋ฐํํด์ฃผ๋ ํจ์์ด๋ค. ๋ค์๋งํด์ ๋ฆฌ์คํธ์ ์ฐ๊ฒฐ๋ ์์๊ฐ ๋ช ๊ฐ์ธ์ง ํ์ ํ๋ค.
- ๋ฐํ๊ฐ์ด intํ์ด๊ธฐ ๋๋ฌธ์ while๋ฌธ์ผ๋ก ์ฝ๊ฒ ๋ฐํ์ด ๊ฐ๋ฅํ๋ค.
t_list *ft_lstlast(t_list *lst)
{
if (lst == NULL)
return (NULL);
while (lst->next != NULL)
{
lst = lst->next;
}
return (lst);
}
// ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ํด์ ํ๋ ํจ์
void free_list(t_list *lst)
{
t_list *current;
t_list *next;
current = lst;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
}
// ํ
์คํธ ์ฝ๋
int main(void)
{
t_list *head = NULL;
t_list *last_node;
// Test case 1: ๋น ๋ฆฌ์คํธ์ ๋ง์ง๋ง ๋
ธ๋
last_node = ft_lstlast(head);
if (last_node == NULL)
{
printf("Test 1: Last node = NULL (Expected: NULL)\n");
}
else
{
printf("Test 1: Unexpected result. Last node is not NULL.\n");
}
// Test case 2: ๋
ธ๋ 1๊ฐ์ธ ๋ฆฌ์คํธ์ ๋ง์ง๋ง ๋
ธ๋
head = ft_lstnew("First Node");
last_node = ft_lstlast(head);
if (last_node != NULL)
{
printf("Test 2: Last node content = %s (Expected: 'First Node')\n",
(char *)last_node->content);
}
else
{
printf("Test 2: Last node is NULL (Unexpected).\n");
}
// Test case 3: ๋
ธ๋ 3๊ฐ ์ถ๊ฐ ํ ๋ง์ง๋ง ๋
ธ๋ ์ฐพ๊ธฐ
head = ft_lstnew("Second Node");
head = ft_lstnew("Third Node");
last_node = ft_lstlast(head);
if (last_node != NULL)
{
printf("Test 3: Last node content = %s (Expected: 'Third Node')\n",
(char *)last_node->content);
}
else
{
printf("Test 3: Last node is NULL (Unexpected).\n");
}
// ๋ฉ๋ชจ๋ฆฌ ํด์
free_list(head);
return (0);
}
๋ฆฌ์คํธ์ ๋ง์ง๋ง์์๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค.
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *current;
if (lst == 0 || new == 0)
return ;
if (*lst == 0)
{
*lst = new;
return ;
}
current = *lst;
while (current->next)
{
current = current->next;
}
current->next = new;
}
// ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ถ๋ ฅํ๋ ํจ์
void print_list(t_list *lst)
{
t_list *current;
current = lst;
while (current != NULL)
{
printf("%s -> ", (char *)current->content);
current = current->next;
}
printf("NULL\n");
}
// ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ํด์ ํ๋ ํจ์
void free_list(t_list *lst)
{
t_list *current;
t_list *next;
current = lst;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
}
// ํ
์คํธ ์ฝ๋
int main(void)
{
t_list *new_node;
t_list *head = NULL; // ์ด๊ธฐ ๋ฆฌ์คํธ๋ ๋น์ด ์์
// Test case 1: ๋น ๋ฆฌ์คํธ์ ์ ๋
ธ๋ ์ถ๊ฐ
new_node = ft_lstnew("First Node");
ft_lstadd_back(&head, new_node);
printf("Test 1: ");
print_list(head); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "First Node -> NULL"
// Test case 2: ๊ธฐ์กด ๋ฆฌ์คํธ์ ๋์ ์ ๋
ธ๋ ์ถ๊ฐ
new_node = ft_lstnew("Second Node");
ft_lstadd_back(&head, new_node);
printf("Test 2: ");
print_list(head); // ๊ธฐ๋ ๊ฒฐ๊ณผ: "First Node -> Second Node -> NULL"
// Test case 3: ๋ฆฌ์คํธ์ ๋์ ๋ ๋ค๋ฅธ ์ ๋
ธ๋ ์ถ๊ฐ
new_node = ft_lstnew("Third Node");
ft_lstadd_back(&head, new_node);
printf("Test 3: ");
print_list(head);
// ๊ธฐ๋ ๊ฒฐ๊ณผ: "First Node -> Second Node -> Third Node -> NULL"
// ๋ฉ๋ชจ๋ฆฌ ํด์
free_list(head);
return (0);
}
๋ฆฌ์คํธ์ ๊ฐ์ฅ ๋ง์ง๋ง ๋ถ๋ถ tail์ new๋ฅผ ์ถ๊ฐํ๋ ํจ์์ด๋ค.
- ๋ง์ง๋ง ์์์ ์ ๊ทผํ๋ lstlast๋ฅผ ์ฌ์ฉํด๋ ๋๊ณ ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ ๊ทผํด๋ ๋๋ค.
- head๋ถ๋ถ์ด null์ธ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ ํ์
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (lst == 0 || del == 0)
return ;
del(lst->content);
free(lst);
}
ํด์ ๊ทธ๋๋ก lst(๋ฆฌ์คํธ) del(์ญ์ ) one(ํ๋) ์ฆ, ์ฐ๊ฒฐ๋ฆฌ์คํธ์ค ํ๋๋ฅผ ์ง์ฐ๋ ํจ์์ด๋ค.
- ์ฒซ ๋ฒ์งธ ์ธ์๊ฐ์ผ๋ก ๋ฐ์ ์์๋ฅผ ๋๋ฒ์งธ ์ธ์ ํจ์ํฌ์ธํฐ๋ก ํด์ ํ๊ณ ๋ฉ๋ชจ๋ฆฌ ์์ฒด๋ ํด์ ํ๋ค.
next ๋ค์์ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ ํด์ ํ๋ฉด ์๋๋ค.
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *now;
if (*lst == 0 || del == 0)
return ;
while (*lst)
{
now = (*lst)->next;
del((*lst)->content);
free(*lst);
*lst = now;
}
}
์ธ์๊ฐ์ผ๋ก ๋ฐ์ ๋ฆฌ์คํธ์ ์ ๊ทผํ์ฌ ๋ฆฌ์คํธ์ ๋ชจ๋ content๋ฅผ ์ญ์ ํ๊ณ ํด์ ํ๋ค.
- ์ฐ์ฐ์ ์ฐ์ ์์ ์ฃผ์ํ๊ธฐ
- ๋๊ธ๋ง ํฌ์ธํฐ ์ด๊ธฐํ ํ์
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (lst == 0)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}
lst๋ฅผ ์ํํ๋ฉฐ ๋ชจ๋ content์์์ ํจ์ f()๋ฅผ ์ ์ฉ์ํค๋ ํจ์์ด๋ค.
- ๊ฐ์ ๋งฅ๋ฝ์ผ๋ก ๋ฐ๋ณต์ ์ผ๋ก ์ํํ๋ฉฐ ์์์ ์ ๊ทผํ๋ค. sizeํจ์์ ๊ฐ์
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *newnode;
t_list *node;
node = 0;
while (lst)
{
newnode = ft_lstnew((*f)(lst->content));
if (newnode == 0)
{
ft_lstclear(&node, del);
return (0);
}
ft_lstadd_back(&node, newnode);
lst = lst->next;
}
return (node);
}
์ธ์์ธ lst๋ฆฌ์คํธ์ content๊ฐ์ ํจ์ f()์ ์ฉ์์ผ์ ์๋ก์ด ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค.
- ์ง๊ธ๊น์ง ๋ง๋ค์ด์ง lstํจ์๋ค์ ํ์ฉํ์ฌ ํ์ดํ๋ค.
#ifndef LIBFT_H
# define LIBFT_H
# include <stdlib.h>
# include <unistd.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_toupper(int c);
int ft_tolower(int c);
size_t ft_strlen(const char *s);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strnstr(const char *big, const char *little,
size_t len);
char *ft_strdup(const char *s);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
char *ft_substr(char const *s, unsigned int start, size_t len);
void ft_striteri(char *s, void (*f)(unsigned int, char *));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
void *ft_memset(void *s, int c, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void ft_bzero(void *s, size_t n);
int ft_atoi(const char *nptr);
char *ft_itoa(int n);
void *ft_calloc(size_t nmemb, size_t size);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *),
void (*del)(void *));
#endif
NAME = libft.a
SRCS = ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c \
ft_isprint.c ft_strlen.c ft_memset.c ft_bzero.c ft_memcpy.c \
ft_memmove.c ft_strlcpy.c ft_strlcat.c ft_toupper.c ft_tolower.c \
ft_strchr.c ft_strrchr.c ft_strncmp.c ft_memchr.c ft_memcmp.c \
ft_strnstr.c ft_atoi.c ft_calloc.c ft_strdup.c ft_substr.c \
ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \
ft_striteri.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c
BNS_SRCS = ft_lstnew_bonus.c ft_lstsize_bonus.c ft_lstadd_back_bonus.c ft_lstadd_front_bonus.c \
ft_lstclear_bonus.c ft_lstdelone_bonus.c ft_lstiter_bonus.c ft_lstlast_bonus.c ft_lstmap_bonus.c
OBJS = $(SRCS:%.c=%.o)
BONUS_OBJS = $(BNS_SRCS:%.c=%.o)
CFLAGS = -Wall -Wextra -Werror -I.
ifdef WITH_BONUS
OBJ_FILE = $(OBJS) $(BONUS_OBJS)
else
OBJ_FILE = $(OBJS)
endif
all: $(NAME)
$(NAME): $(OBJ_FILE)
ar -rcs $@ $^
%.o: %.c
cc $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS) $(BONUS_OBJS)
fclean:
make clean
rm -f $(NAME)
re:
make fclean
make all
bonus: $(OBJ_FILE)
@make WITH_BONUS=1 all
.PHONY : all clean fclean re bonus