linked list 설명
배열(Array) & 연결리스트(Linked List)
해당 페이지에 설명해두었음.
ft_lstadd_front를 구현하기 위해 libft.h파일에
typedef struct s_list { void *content; struct s_list *next; } t_list;이 구조체를 추가해야한다.
void ft_lstadd_front(t_list **lst, t_list *new)
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (new == NULL)
return ;
new->next = *lst;
*lst = new;
}
new노드의 next포인터를 기존 리스트의 첫 번째 노드를 가리키게 한다.*lst)를 new로 바꾼다.int main(void)
{
t_list *list = NULL;
t_list *node1;
t_list *node2;
node1 = ft_lstnew("first");
node2 = ft_lstnew("second");
ft_lstadd_front(&list, node1); // 리스트: first
ft_lstadd_front(&list, node2); // 리스트: second -> first
printf("First node content: %s\n", (char *)list->content); // "second"
printf("Second node content: %s\n", (char *)list->next->content); // "first"
free(node1);
free(node2);
return (0);
}