ㅇㅁㄴㅇ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include "LinkedList.h" void merge(Node** Ahead, Node** Bhead, Node** Chead) { Node* tempA = *Ahead; Node* tempB = *Bhead; while (tempA != NULL || tempB != NULL) { if (tempB == NULL || (tempA != NULL && tempA->data < tempB->data)) { insert(Chead, createNode(tempA->data)); //2회 tempA = tempA->link; } else { insert(Chead, createNode(tempB->data)); tempB = tempB->link; } } } int main(void) { int i = 0; Node* head1 = NULL; Node* head2 = NULL; Node* head3 = NULL; for (int i = 0; i < 10; i++) { insert(&head1, createNode(i * 2)); insert(&head2, createNode(i * 2 + 1)); } printList(&head1); printf("\n"); printList(&head2); printf("\n"); merge(&head1, &head2, &head3); printList(&head3); return 0; } Node* searchData(Node** Head, int data) { if (isEmpty(Head)) { printf("노드가 비었습니다"); return NULL; } else { Node* temp = *Head; while (temp) { if (temp->data == data) { printf("데이터값을 찾았습니다."); return temp; } temp = temp->link; } } printf("찾는 데이터가 없습니다."); return NULL; } void insertAfterNode(Node** head, Node* p, Node* pn) { Node* temp = *head; Node* temp2 = NULL; while (temp->data == p->data) { temp = temp->link; } temp2 = temp->link; temp->link = pn; pn->link = temp2; } Node* createNode(element e) { Node* temp = (Node*)malloc(sizeof(Node)); temp->link = NULL; temp->data = e; return temp; } //생성된 노드의 주솟값을 리턴 Node* insert(Node** Head, Node* p) { if (Head == NULL) { printf("No head pointer is assignend!\n"); } if (isEmpty(Head)) { *Head = p; } else { Node* temp = *Head; while (temp->link) { temp = temp->link; } temp->link = p; } return p; } bool isEmpty(Node** Head) { if (!*Head) return true; return false; } int countList(Node** Head) { int count = 0; Node* temp = *Head; while (temp) { count++; temp = temp->link; } return count; } void printList(Node** Head) { if (isEmpty(Head)) { printf("없음"); return; } Node* temp = *Head; while (temp) { printf("%d, ", temp->data); temp = temp->link; } } | cs |
합치는 부분이 기본적으로 병합정렬에 Merge 부분와 똑같다
'자료구조' 카테고리의 다른 글
스택 , 큐, 트리 그리고 깊이탐색과 넓이탐색 (0) | 2018.05.26 |
---|---|
Double Linked List (0) | 2018.05.26 |
List 자료구조의 기본적 코드 (0) | 2018.05.26 |
트리 연산과 깊이 및 넓이탐색 (0) | 2018.05.26 |
스택을 이용한 미로찾기 (0) | 2018.05.26 |