[libft] 나만의 C라이브러리 만들기 Bonus

leocodms·2020년 12월 27일
0

42seoul

목록 보기
6/10

1.ft_lstnew

#include "libft.h"

t_list		*ft_lstnew(void *content)
{
	t_list	*new;

	if (!(new = (t_list *)malloc(sizeof(t_list))))
		return (0);
	new -> content = content;
	new -> next = NULL;
	return (new);
}
  • 기능 : 구조체 t_list를 동적할당하고, 인자값으로 content변수를 초기화, next변수는 null로 초기화 한다.
  • return : 생성한 t_list

2.ft_lstadd_front

#include "libft.h"

void	ft_lstadd_front(t_list **lst, t_list *new)
{
	if (lst == 0 || new == 0)
		return ;
	new->next = *lst;
	*lst = new;
}
  • 기능 : 시작 부분에 new 요소를 추가한다.
  • 링크드 리스트 개념이다. lst가 가리키는 t_list앞에 new를 추가하면 된다.

3.ft_lstsize

#include "libft.h"

int		ft_lstsize(t_list *lst)
{
	int	i;

	i = 0;
	while (lst)
	{
		lst = lst->next;
		i++;
	}
	return (i);
}
  • 기능 : lst로 주어진 노드와 연결된 노드의 개수를 체크한다.
  • return : 링크드 리스트의 길이
  • 풀이 : 인자로 주어지는 값이 링크드리스트의 한 노드이기 때문에 그 노드를 타고 들어가서 끝까지 가야한다. 따라서 lst를 계속 다음 노드로 새로 갱신해주면서 개수를 세야한다.

4.ft_lstlast

#include "libft.h"

t_list		*ft_lstlast(t_list *lst)
{
	while (lst->next != NULL)
		lst = lst->next;
	return (lst);
}
  • 기능 : 링크드 리스트의 마지막 노드를 반환한다.
  • return : 리스트의 마지막 노드.

5.ft_lstadd_back

#include "libft.h"

void		ft_lstadd_back(t_list **lst, t_list *new)
{
	t_list	*last;

	if (lst == NULL || new == NULL)
		return ;
	if (*lst == NULL)
	{
		*lst = new;
		return ;
	}
	last = ft_lstlast(*lst);
	new->next = NULL;
	last->next = new;
}
  • 기능 : 링크드 리스트 맨 뒤에 노드를 추가한다.
  • return : void
  • 풀이 :
    인자로 주어진 리스트가 빈 리스트 이라면, new 노드를 리스트에 넣어주고 반환한다. 아니라면, ft_lstlast로 마지막 노드의 위치를 구하고 그 노드의 next 변수에 new 노드를 이어준다.

6.ft_lstdelone

#include "libft.h"

void		ft_lstdelone(t_list *lst, void (*del)(void *))
{
	del(lst->content);
	free(lst);
}
  • 기능 : del 함수로 노드의 conden를 삭제하고 노드 메모리 할당 해제

7.ft_lstclear

#include "libft.h"

void		ft_lstclear(t_list **lst, void (*del)(void *))
{
	t_list	*curr;
	t_list	*next;

	if (*lst == NULL)
		return ;
	curr = *lst;
	while (curr)
	{
		next = curr->next;
		del(curr->content);
		free(curr);
		curr = next;
	}
	*lst = NULL;
}
  • 기능 : del함수를 사용하여 링크드 리스트 전체 메모리 해제.
  • 풀이 :
    현재 노드를 가리키는 변수와, 다음 노드를 가리키는 변수를 선언해서 진행.
    마지막에 모든 노드를 삭제해 주고 리스트를 NULL로 초기화 해줌.

8.ft_lstiter

#include "libft.h"

void		ft_lstiter(t_list *lst, void (*f)(void *))
{
	if (lst == NULL || f == NULL)
		return ;
	while (lst)
	{
		f(lst->content);
		lst = lst->next;
	}
}
  • 기능 : 링크드 리스트를 순환하며, content 변수에 f 함수를 적용한다.

9.ft_lstmap

#include "libft.h"

t_list		*ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
	t_list	*new;
	t_list	*curr;
	t_list	*temp;

	if (lst == NULL || f == NULL)
		return (0);
	if (!(new = ft_lstnew(f(lst->content))))
		return (0);
	curr = new;
	lst = lst->next;
	while (lst)
	{
		if (!(temp = ft_lstnew(f(lst->content))))
		{
			ft_lstclear(&new, del);
			return (0);
		}
		curr->next = temp;
		curr = curr->next;
		lst = lst->next;
	}
	return (new);
 }
  • 기능 : 링크드 리스트 각 노드의 content에 f 함수를 적용한 새로운 리스트를 만들고, 새 노드를 동적할당한다. 할당 실패시 전에 만들어 놓았던 노드들은 모두 삭제하고 0반환
  • return : 새로 생성된 리스트
  • 풀이 :
    링크드 리스트 문제들 중 가장 난이도 있는 문제였다. 그러나 함수의 정확한 동작을 이해한다면 쉽게 풀 수 있다. 이 문제의 핵심은 적절한 포인터 변수들의 사용이다.
profile
Backend Developer

0개의 댓글