04. 연결리스트(Linked List) – 연결 리스트 구현
- 연결 리스트(Linked List) 구현 #1 : S004_LinkedList.c
12345678910111213#include <stdio.h>#include <stdlib.h>typedef struct _node{int data;struct _node *next;} Node;Node *head = NULL;void main(int argc, char* argv[]){} - 연결 리스트(Linked List) 구현 #2
12345678910111213void Free(){Node *cur;while(head != NULL) {cur = head;head = head->next;free(cur);}}void main(int argc, char* argv[]){Free();} - 연결 리스트(Linked List) 구현 #3
1234567891011121314151617181920212223void Insert(int data){Node *cur;Node *newNode = (Node *)malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;if(head == NULL){head = newNode;}else{cur = head;while(cur->next != NULL){cur = cur->next;}cur->next = newNode;}}void main(int argc, char* argv[]){Insert(15);Free();} - 연결 리스트(Linked List) 구현 #4
1234567891011121314void PrintList(){Node *cur = head;while(cur != NULL){printf("%d ", cur->data);cur = cur->next;}printf("\n\n");}void main(int argc, char* argv[]){Insert(15); PrintList();Insert(12); PrintList();Free();} - 연결 리스트(Linked List) 구현 #5
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748void RemoveData(int data){int n = 0;if(head == NULL){printf("리스트가 비어있어 삭제할 수 없습니다.\n");return;}else{Node *cur = head;Node *curNext = head->next;if(cur->data == data){free(cur);head = curNext;printf("%d번째 위치에서 %d를삭제했습니다.\n", n, data);return;}else{while(cur->next != NULL){n++;if(curNext->data == data){cur->next = curNext->next;free(curNext);printf("%d번째 위치에서 %d를삭제했습니다.\n", n, data);return;}cur = cur->next;curNext = curNext->next;}printf("리스트에 %d가 존재하지 않습니다.\n", data);}}}void main(int argc, char* argv[]){Insert(15); PrintList();Insert(12); PrintList();Insert(36); PrintList();Insert(22); PrintList();Insert(32); PrintList();RemoveData(36); PrintList();RemoveData(32); PrintList();RemoveData(20); PrintList();RemoveData(22); PrintList();RemoveData(15); PrintList();RemoveData(12); PrintList();Free();} - 연결 리스트(Linked List) 완성 버전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111#include <stdio.h>#include <stdlib.h>typedef struct _node{int data;struct _node *next;} Node;Node *head = NULL;void Insert(int data){Node *cur;Node *newNode = (Node *)malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;if(head == NULL){head = newNode;}else{cur = head;while(cur->next != NULL){cur = cur->next;}cur->next = newNode;}}void RemoveData(int data){int n = 0;if(head == NULL){printf("리스트가 비어있어 삭제할 수 없습니다.\n");return;}else{Node *cur = head;Node *curNext = head->next;if(cur->data == data){free(cur);head = curNext;printf("%d번째 위치에서 %d를삭제했습니다.\n", n, data);return;}else{while(cur->next != NULL){n++;if(curNext->data == data){cur->next = curNext->next;free(curNext);printf("%d번째 위치에서 %d를삭제했습니다.\n", n, data);return;}cur = cur->next;curNext = curNext->next;}printf("리스트에 %d가 존재하지 않습니다.\n", data);}}}void PrintList(){Node *cur = head;while(cur != NULL){printf("%d ", cur->data);cur = cur->next;}printf("\n\n");}void PrintList2(){if(head == NULL){return;}else{Node *cur = head;printf("%d ", cur->data);while(cur->next != NULL){cur = cur->next;printf("%d ", cur->data);}printf("\n\n");}}void Free(){Node *cur;while(head != NULL) {cur = head;head = head->next;free(cur);}}void main(int argc, char* argv[]){Insert(15); PrintList();Insert(12); PrintList();Insert(36); PrintList();Insert(22); PrintList();Insert(32); PrintList();RemoveData(36); PrintList();RemoveData(32); PrintList();RemoveData(20); PrintList();RemoveData(22); PrintList();RemoveData(15); PrintList();RemoveData(12); PrintList();Free();} - 정렬 삽입 함수 InsertSort() 추가 후
- Insert(15) -> InsertSort(15) 수정
- Insert(12) -> InsertSort(12) 수정
- Insert(36) -> InsertSort(36) 수정
- 입력되는 값의 오름차순으로 입력되도록 수정