<틀린 부분, 부족한 부분 언제든지 댓글 달아주세요. 아직 코린이입니다.>
The following functions will allow you to easily use your lists.
t_list struct
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
- t_list : 연결리스트의 노드 구조체
- content : 데이터를 저장할 포인터
- next : 다음 노드의 주소를 저장할 포인터
1. ft_lstnew
t_list *ft_lstnew(void *content)
description
- Allocates (with malloc(3)) and returns a new element.
- The variable ’content’ is initialized with the value of the parameter ’content’.
- The variable ’next’ is initialized to NULL.
return
- The new element
- t_list 크기의 새 배열 할당
- 새 배열을 new라고 할 때
- new->content = content : t_list인 new의 content지정
- new->next = NULL : 다음 구조체 지정
2. ft_lstadd_front
void ft_lstadd_front(t_list **lst, t_list *new)
description
- Adds the element ’new’ at the beginning of the list.
- 새 노드의 다음 주소를 head로 설정
- head를 새 노드로 설정
- 연결 리스트 구현 시 이중 포인터 사용 이유
- 단일 연결리스트 삽입 및 삭제 head 포인터 값 변화
- 호출 함수의 포인터변수가 참조하는 객체를 피호출 함수로 바꾸고자 할 경우 사용
- t_list **lst : t_list 포인터 lst의 주소 가리키는 포인터
- *lst는 head의 주소
3. ft_lstsize
int ft_lstsize(t_list *lst)
description
- Counts the number of elements in a list
return
- Length of the list
- lst의 주소를 lst->next를 통해서 옮겨주는 방법으로 길이 구하기
4. ft_lstlast
t_list *ft_lstlast(t_list *lst)
description
- Returns the last element of the list
return
- Last element of the list
- lst->next != NULL까지 lst 주소 이동
5. ft_lstadd_back
void ft_lstadd_back(t_list **lst, t_list *new)
description
Add the element
newat the end of the list
- void 함수에서의 return ;의 의미
- 함수 멈추는 역할
- *lst == NULL 일 경우 바로 new 넣어주면 된다.
- 아닐 경우 주소 끝가지 간 후 *lst->next = new로 지정
6. ft_lstdelone
void ft_lstdelone(t_list *lst, void (*del)(void *))
description
- Takes as a parameter an element and frees the memory of the element’s content using the function ’del’ given as a parameter and free the element.
- The memory of ’next’ must not be freed.
- del함수 : lst의 content의 값 삭제
- 이후 free해주면 lst->content 접근 못함.
7. ft_lstclear
void ft_lstclear(t_list **lst, void (*del)(void *))
description
- Deletes and frees the given element and every successor of that element, using the function ’del’ and free(3).
- Finally, the pointer to the list must be set to NULL.
- 다음 값 미리 저장해 놓은 후 lstdelone을 통해서 현재 값 제거
- 현재값은 저장해놓은 값으로 reset
- 지워진 후 head == NULL
8. ft_lstiter
void ft_lstiter(t_list *lst, void (*f)(void *))
description
- Iterates the list ’lst’ and applies the function ’f’ to the content of each element.
- 리스트 반복하면서 특정함수 적용
9. ft_lstmap
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
description
- Iterates the list ’lst’ and applies the function ’f’ to the content of each element.
- Creates a new list resulting of the successive applications of the function ’f’.
- The ’del’ function is used to delete the content of an element if needed.
return
- The new list.
- NULL if the allocation fails.
- 저장해야하는 구조체 NULL로 시작
- lstnew함수로 넣고 lstadd_back으로 계속 뒤에 넣어줌
- del함수는 lstnew함수 실패했을 경우 사용
'코딩 > C' 카테고리의 다른 글
| Libft (Part 2 : Additional functions) (0) | 2021.02.06 |
|---|---|
| Libft (Part 1 : Libc Function) (0) | 2020.12.30 |