▶ 연결리스트 기초
#include < stdio.h > #include < stdlib.h >
typedef struct node { char data; struct node *next; }NODE;
int main() { NODE *list; NODE *temp;
list = (NODE *)malloc(sizeof(NODE)); list->data = 'a'; list->next = (NODE*)malloc(sizeof(NODE)); list->next->data = 'b'; list->next->next = (NODE*)malloc(sizeof(NODE)); list->next->next->data = 'c'; list->next->next->next = NULL; // 연결리스트의 출력 temp = list; while(temp != NULL) { printf("%5c\n", temp->data); temp = temp->next; }
temp = list;
while(temp != NULL) { list = list->next; // 기존의 list 가 다음주소에 따라가야하니깐 free(temp); // next 를 해주고 free()함수로 지워줌 temp = list; // temp 에다가 넘겨준 list 주소를 다시줌 } // 그걸 반복! return 0; } |
|
▶ free() 함수를 쓸 곳의 주소를 잘 그려가며 해야함.