Data Structure. Linked List

13

Transcript of Data Structure. Linked List

Page 1: Data Structure. Linked List
Page 2: Data Structure. Linked List

errorsplitsearchRemove_node2Remove_nodedisplayCreate_nodeAdd_node

main()

int main(){ListNode *list = NULL;

//element추가for(int i=0 ;i<15; i++){//적당한 개수 15개로 Node삽입

add_node(&list, NULL, create_node(i));}//element출력printf("----- add결과 -----\n");display(list);

//element삭제removed_node(&list, NULL, list);removed_node(&list, NULL, list);printf("----- remove결과 -----\n");display(list);

//ListNode쪼개기ListNode *odd_list = NULL;ListNode *even_list = NULL;split(&list, &odd_list, &even_list);//list내 Node들을 odd_list와 even_list로 조건에 맞게 복사하여 저장

printf("----- split결과 -----\n");printf("odd_list : \n");display(odd_list);printf("even_list : \n");display(even_list);printf("원래 list : \n");display(list);

//element검색int key;printf("\nlist내 삭제할 data를 입력하세요 >>");scanf("%d", &key);ListNode *x = search(list, key);//removed_node2로 삭제할 Node 주소

//element삭제2removed_node2(&list, x);printf("----- remove2결과 -----\n");display(list);

}

Page 3: Data Structure. Linked List

main()main()main()main()main()main()main()main()

add_node(ListNode **list, ListNode *pos, ListNode *new_node)

// element 추가void add_node(ListNode **list, ListNode *pos, ListNode *new_node){

if(*list == NULL){//공백List*list = new_node;

}

//선행노드NULL인 첫번째node로 삽입else if(pos == NULL){

new_node->link = *list;*list = new_node;

}

else{//해당 list의 특정 주소를 선행노드로 알고 입력할 경우//1. after_node를 new_node가 가리킴new_node->link = pos->link;//2. before_node가 new_node를 가리킴pos->link = new_node;

}}

Head Pointer

new_node

new_node->link = pos->link;pos->link = new_node;

before_node after_node

Head Pointer after_node

new_nodenew_node->link = *list;

*list = new_node;

Head Pointer\

new_node

*list = new_node;

Page 4: Data Structure. Linked List

errorsplitsearchRemove_node2Remove_nodedisplaymain() add_node

create_node(element data)

// node 메모리 할당ListNode* create_node(element data){

ListNode *p = (ListNode *)malloc(sizeof(ListNode));if(p == NULL)error("메모리 할당 에러");p->data = data;p->link = NULL;return p;

}

Compile Time Binding

Main Memory

8 Byte

*P

\

new_node

Page 5: Data Structure. Linked List

errorsplitsearchRemove_node2Remove_nodemain() add_nodecreate_node

display(ListNode *list)

// element 출력void display(ListNode *list){

while(list != NULL){printf("%d", list->data);list = list->link;if(list != NULL)printf("->");

}printf("\nListNode를 모두 출력하였습니다.\n");

}

Head Pointer

printf("%d", list->data); printf("->");

list = list->link;

list = list->link;

list = list->link;

printf("\nListNode를 모두 출력하였습니다.\n");

printf("%d", list->data); printf("->");

printf("%d", list->data); printf("->");

printf("%d", list->data);

Page 6: Data Structure. Linked List

main()main()main()main()main()main()main()main()

remove_node(ListNode **phead, ListNode *pos, ListNode *removed_node)

void removed_node(ListNode **phead, ListNode *pos, ListNode*removed_node){

ListNode *p = *phead;if(p == NULL) printf("List에 Node가 없습니다.\n");

// 첫번째 Node를 삭제else if(pos == NULL) *phead = p->link;

// 지정한 before_node주소 pos 다음에 위치한 Node를 삭제else pos->link = removed_node->link;

printf("%d삭제\n", removed_node->data);free(removed_node);

}

Head Pointer

after_node

removed_node

*phead = p->link;

printf("%d삭제\n", removed_node->data);free(removed_node);

Head Pointer

before_node after_node

removed_node

pos

pos->link = removed_node->link;

printf("%d삭제\n", removed_node->data);free(removed_node);

Page 7: Data Structure. Linked List

main()main()main()main()main()main()main()main()

remove_node2(ListNode **phead, ListNode *removed_node)

void removed_node2(ListNode **phead, ListNode *x){

// 삭제할 Node 주소가 NULL인 경우 (List내 없는 경우)if(x == NULL)printf("List에 Node가 존재하지 않습니다.\n");

// 마지막 Node라서 after_node가 없는 경우 x바로 삭제else if(x->link == NULL){

ListNode *p = *phead;printf("%d의 다음 node가 없습니다. %d를 바로

삭제하겠습니다.\n", x->data, x->data);while(p->link->link != NULL) p = p->link;p->link = NULL;free(x); // 마지막 Node인 x 삭제

}

else{ListNode *removed_node = x->link;//x다음인 y를 삭제printf("%d가 들어있던 node를 %d의 node로 복사하고 삭제

\n", removed_node->data, x->data);x->data = x->link->data;//데이터 복사x->link = x->link->link;free(removed_node);

}}

Head Pointer

\

ListNode *p = *phead;while(p->link->link != NULL) p = p->link;

p->link = NULL;

free(x); // 마지막 Node인 x 삭제

removed_node

removed_node

x

before_nodex->data = x->link->data;//데이터 복사

x->link = x->link->link;

ListNode *removed_node = x->link;free(removed_node);

Page 8: Data Structure. Linked List

errorsplitmain()add_nodecreate_nodedisplayremove_noderemove_node2

search(ListNode *list, element key)

ListNode* search(ListNode *list, element key){ListNode *p = list;ListNode *found = NULL;//검색된 Node를 저장while(p != NULL){

if(p->data == key){printf("%d를 찾았습니다. 해당 데이터는 메모리 내

%x주소에 존재합니다.\n", p->data, p);found = p;

//포인터변수 x에 대입하기 위해 주소값 리턴return found;}else p = p->link;

}return found;//NULL을 리턴

}

found

Head Pointer

p = p->link;

p = p->link;

p = p->link;

found = p;

element key = data;

Page 9: Data Structure. Linked List

main()main()main()main()main()main()main()main()

split(ListNode **list, ListNode **odd_list, ListNode **even_list)

void split(ListNode** phead, ListNode** phead1, ListNode** phead2) {

//phead1 -> 짝수번째 노드들을 모음.//phead2 -> 홀수번째 노드들을 모음.int count = 0;ListNode* tmp = *phead;while (tmp != NULL) {

++count;if (count % 2 == 0) //짝수

add_node(phead1, NULL, create_node(tmp->data));else //홀수

add_node(phead2, NULL, create_node(tmp->data));tmp = tmp->link;

}

}

짝수Head Pointer \

new_node

add_node(phead1, NULL, create_node(tmp->data));

홀수Head Pointer \

new_node

add_node(phead2, NULL, create_node(tmp->data));

count = 1; count = 2; count = 3;

count = 8; count = 9; count = 4;

count = 7; count = 6; count = 5;

tmp = tmp->link; tmp = tmp->link;

tmp = tmp->link;

tmp = tmp->link;

tmp = tmp->link;tmp = tmp->link;

tmp = tmp->link;

tmp = tmp->link;

Page 10: Data Structure. Linked List

main()add_nodecreate_nodedisplayremove_noderemove_node2search split

error(char *message)

// 메모리 할당 오류 메시지//#include<stdlib.h>

void error(char* message){fprintf(stderr, "%s\n", message);exit(1);

}

Page 11: Data Structure. Linked List
Page 12: Data Structure. Linked List
Page 13: Data Structure. Linked List