linked list 설명
배열(Array) & 연결리스트(Linked List)
해당 페이지에 설명해두었음.
ft_lstadd_back를 구현하기 위해 libft.h파일에
typedef struct s_list { void *content; struct s_list *next; } t_list;이 구조체를 추가해야한다.
void ft_lstadd_back(t_list **lst, t_list *new)
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *res;
res = *lst;
if (*lst == NULL && new != NULL)
{
*lst = new;
return ;
}
else if (lst == NULL || new == NULL)
return ;
while ((*lst)->next != NULL)
{
*lst = (*lst)->next;
}
(*lst)->next = new;
*lst = res;
}
#include "libft.h"
#include <stdio.h>
int main(void)
{
t_list *list = NULL;
t_list *node1;
t_list *node2;
t_list *node3;
node1 = ft_lstnew("node1");
node2 = ft_lstnew("node2");
node3 = ft_lstnew("node3");
ft_lstadd_back(&list, node1); // 리스트: node1
ft_lstadd_back(&list, node2); // 리스트: node1 -> node2
ft_lstadd_back(&list, node3); // 리스트: node1 -> node2 -> node3
while (list)
{
printf("%s\n", (char *)list->content);
list = list->next;
}
return (0);
}