libft - KimTaebin-ai/study_posts GitHub Wiki

ํ•„์š” ๊ฐœ๋… ๋ฐ ํ•™์Šต ๋‚ด์šฉ

libft

๋ชจ์˜ ํ…Œ์ŠคํŠธ ๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•

bash ~/Libftest/grademe.sh

Part1

C์˜ ํ‘œ์ค€๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Libc์˜ ํ•จ์ˆ˜๋“ค์„ ์ด๋ฏธํ…Œ์ด์…˜ํ•˜์—ฌ ๋งŒ๋“ ๋‹ค.

isalpha

int	ft_isalpha(int c)
{
	if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
		return (1);
	else
		return (0);
}

์„ค๋ช… ์ƒ๋žต

isdigit

int	ft_isdigit(int c)
{
	if (c >= '0' && c <= '9')
		return (1);
	else
		return (0);
}

์„ค๋ช… ์ƒ๋žต

isalnum

int	ft_isalnum(int c)
{
	if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0'
			&& c <= '9'))
		return (1);
	else
		return (0);
}

์„ค๋ช… ์ƒ๋žต

isascii

int	ft_isascii(int c)
{
	if (c >= 0 && c <= 127)
		return (1);
	else
		return (0);
}

์„ค๋ช… ์ƒ๋žต

isprint

int	ft_isprint(int c)
{
	if (c >= 32 && c <= 126)
		return (1);
	else
		return (0);
}

์„ค๋ช… ์ƒ๋žต

strlen

size_t	ft_strlen(const char *s)
{
	size_t	len;

	len = 0;
	while (s[len] != '\0')
		len++;
	return (len);
}

๋ฌธ์ž์—ด์„ ์ธ์ž๋กœ ๋ฐ›์•„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ํŒŒ๋ผ๋ฏธํ„ฐ ์ธ์ž๋กœ ๋ฐ›๋Š” const char * s ์ƒ์ˆ˜ ํ‚ค์›Œ๋“œ. ์ฆ‰, ์›๋ณธ ๋ฐ์ดํ„ฐ๋Š” ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์„ค๊ณ„ ๋ชฉ์ ์ธ ์  ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹๋‹ค

memset

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 ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค.

bzero

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์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

memcpy

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๊ฐ€ ๊ฒน์น˜๋ฉด ๋™์ž‘์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.

memmove

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 ์™€ ๋‹ค๋ฅด๊ฒŒ ๋‘ ๋ฌธ์ž์—ด์ด ๊ฒน์น  ์ˆ˜ ์žˆ๊ณ , ๋ณต์‚ฌ๋œ ๋‚ด์šฉ๋„ ํ›ผ์†๋˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

strlcpy

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);
}

strlcat

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);
}

toupper

int	ft_toupper(int c)
{
	int	diff;

	diff = 'A' - 'a';
	if (c >= 'a' && c <= 'z')
		c += diff;
	return (c);
}

tolower

int	ft_tolower(int c)
{
	int	diff;

	diff = 'A' - 'a';
	if (c >= 'A' && c <= 'Z')
		c -= diff;
	return (c);
}

strchr

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);
}

strrchr

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);
}

strncmp

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);
}

memchr

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);
}

memcmp

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);
}

strnstr

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);
}

atoi

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 ์‚ฝ์งˆํ•œ ์„ ๋ฐฐ๋‹˜ ์ž๋ฃŒ

์›๋ฆฌ

calloc

#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);
}

strdup

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);
}

Part2

libc์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ํฌํ•จ๋œ ํ•จ์ˆ˜๋“ค์„ ์žฌ๊ตฌํ˜„ํ•œ๋‹ค.

ft_substr

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 ์ฒ˜๋ฆฌํ•ด์•ผํ•จ

ft_strjoin

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์˜ ๊ธธ์ด์ฒดํฌ ๋ฐ ๋„๋ฌธ์ž ์‚ฝ์ž…

ft_strtrim

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์„ ๊ฒ€์‚ฌํ•˜์—ฌ ํ’€์ดํ•œ๋‹ค.

ft_split

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์„ ์–ธํ•˜๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•  ๊ฒƒ

ft_itoa

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์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์™€ ์ˆซ์ž๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•จ

ft_strmapi

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์ฒ˜๋ฆฌ ์ค‘์š”

ft_striteri

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 ํ‘œ์ค€ ์—๋Ÿฌ

ft_putchar_fd

void	ft_putchar_fd(char c, int fd)
{
	write(fd, &c, 1);
}

putchar_fdํ•จ์ˆ˜๋Š” ํ•ด๋‹นํ•˜๋Š” fd(ํŒŒ์ผ๋””์Šคํฌ๋ฆฝํ„ฐ ๊ฐ’)์œผ๋กœ ์—ด๋ ค์žˆ๋Š” ์ŠคํŠธ๋ฆผ์— ๋ฐ”์ดํŠธ์—ด์„ c๋ฌธ์ž๋ฅผ ํ˜๋ ค๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ft_putstr_fd

void	ft_putstr_fd(char *s, int fd)
{
	if (s == 0)
		return ;
	write(fd, s, ft_strlen(s));
}

putstr_fdํ•จ์ˆ˜๋Š” ํ•ด๋‹นํ•˜๋Š” fd(ํŒŒ์ผ๋””์Šคํฌ๋ฆฝํ„ฐ ๊ฐ’)์œผ๋กœ ์—ด๋ ค์žˆ๋Š” ์ŠคํŠธ๋ฆผ์— ๋ฐ”์ดํŠธ์—ด์„ ๋ฌธ์ž์—ด s๋ฅผ ํ˜๋ ค๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ft_putendl_fd

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โ€™)์„ ์ถ”๊ฐ€ํ•œ ํ•จ์ˆ˜์ด๋‹ค.

ft_putnbr_fd

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ํ•จ์ˆ˜๋Š” ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ˆซ์ž๋กœ ๋ฐ”๊พธ์„œ ์ŠคํŠธ๋ฆผ์— ํ˜๋ ค๋ณด๋‚ธ๋‹ค.

Bonus part

ft_lstnew

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์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ft_lstadd_front

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๋กœ ์ ‘๊ทผํ•˜์—ฌ ์—ฐ๊ฒฐํ•œ๋‹ค.

ft_lstsize

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๋ฌธ์œผ๋กœ ์‰ฝ๊ฒŒ ๋ฐ˜ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ft_lstlast

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);
}

๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ft_lstadd_back

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์ธ ๊ฒฝ์šฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š”

ft_lstdelone

void	ft_lstdelone(t_list *lst, void (*del)(void *))
{
	if (lst == 0 || del == 0)
		return ;
	del(lst->content);
	free(lst);
}

ํ•ด์„ ๊ทธ๋Œ€๋กœ lst(๋ฆฌ์ŠคํŠธ) del(์‚ญ์ œ) one(ํ•˜๋‚˜) ์ฆ‰, ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์ค‘ ํ•˜๋‚˜๋ฅผ ์ง€์šฐ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ’์œผ๋กœ ๋ฐ›์€ ์š”์†Œ๋ฅผ ๋‘๋ฒˆ์งธ ์ธ์ž ํ•จ์ˆ˜ํฌ์ธํ„ฐ๋กœ ํ•ด์ œํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์ž์ฒด๋„ ํ•ด์ œํ•œ๋‹ค.

next ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋Š” ํ•ด์ œํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

ft_lstclear

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๋ฅผ ์‚ญ์ œํ•˜๊ณ  ํ•ด์ œํ•œ๋‹ค.

  • ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„ ์ฃผ์˜ํ•˜๊ธฐ
  • ๋Œ•๊ธ€๋ง ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™” ํ•„์ˆ˜

ft_lstiter

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ํ•จ์ˆ˜์™€ ๊ฐ™์Œ

ft_lstmap

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
โš ๏ธ **GitHub.com Fallback** โš ๏ธ