05장 연결리스트 3 – 양방향 연결리스트
- 양방향 연결리스트(Double Linked List) 구현 #1 : S005_DoubleLinkedList.c
12345678910111213#include <stdio.h>#include <stdlib.h>typedef struct _node{int data;struct _node *next;struct _node *prev;} Node;Node *head = NULL;void main(int argc, char* argv[]){} - 양방향 연결리스트(Double Linked List) 구현 #2
12345678910111213void Free(){Node *cur;while(head != NULL) {cur = head;head = head->next;free(cur);}}void main(int argc, char* argv[]){Free();} - 양방향 연결리스트(Double Linked List) 구현 #3
1234567891011121314151617181920212223void Insert(int data){Node *cur;Node *newNode = (Node *)malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;newNode->prev = NULL;if(head == NULL){head = newNode;}else{Node *cur = head;cur->prev = newNode;newNode->next = cur;head = newNode;}}void main(int argc, char* argv[]){Insert(15);Free();} - 양방향 연결리스트(Double Linked List) 구현 #4
1234567891011121314151617181920212223242526272829void PrintList(){Node *cur = head;while(cur != NULL){printf("%d ", cur->data);cur = cur->next;}printf("\n\n");}void PrintListRev(){Node *cur = head;Node *curPrev = NULL;while(cur != NULL){curPrev = cur;cur = cur->next;}while(curPrev != NULL){printf("%d ", curPrev->data);curPrev = curPrev->prev;}printf("\n\n");}void main(int argc, char* argv[]){Insert(15); PrintList(); PrintListRev();Insert(12); PrintList(); PrintListRev();Free();} - 양방향 연결리스트(Double Linked List) 구현 #5
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051void 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);if(curNext != NULL)curNext->prev = NULL;head = curNext;printf("%d번째 위치에서 %d를삭제했습니다.\n", n, data);return;}else{while(cur->next != NULL){n++;if(curNext->data == data){cur->next = curNext->next;Node *curNextNext = curNext->next;if(curNextNext != NULL)curNextNext->prev = curNext->prev;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(); PrintListRev();Insert(12); PrintList(); PrintListRev();Insert(36); PrintList(); PrintListRev();Insert(22); PrintList(); PrintListRev();Insert(32); PrintList(); PrintListRev();RemoveData(36); PrintList(); PrintListRev();RemoveData(32); PrintList(); PrintListRev();RemoveData(22); PrintList(); PrintListRev();RemoveData(15); PrintList(); PrintListRev();RemoveData(12); PrintList(); PrintListRev();Free();} - 양방향 연결리스트(Double Linked List) 완성 버전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116#include <stdio.h>#include <stdlib.h>typedef struct _node{int data;struct _node *next;struct _node *prev;} Node;Node *head = NULL;void Insert(int data){Node *cur;Node *newNode = (Node *)malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;newNode->prev = NULL;if(head == NULL){head = newNode;}else{Node *cur = head;cur->prev = newNode;newNode->next = cur;head = 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);if(curNext != NULL)curNext->prev = NULL;head = curNext;printf("%d번째 위치에서 %d를삭제했습니다.\n", n, data);return;}else{while(cur->next != NULL){n++;if(curNext->data == data){cur->next = curNext->next;Node *curNextNext = curNext->next;if(curNextNext != NULL)curNextNext->prev = curNext->prev;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 PrintListRev(){Node *cur = head;Node *curPrev = NULL;while(cur != NULL){curPrev = cur;cur = cur->next;}while(curPrev != NULL){printf("%d ", curPrev->data);curPrev = curPrev->prev;}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(); PrintListRev();Insert(12); PrintList(); PrintListRev();Insert(36); PrintList(); PrintListRev();Insert(22); PrintList(); PrintListRev();Insert(32); PrintList(); PrintListRev();RemoveData(36); PrintList(); PrintListRev();RemoveData(32); PrintList(); PrintListRev();RemoveData(20); PrintList(); PrintListRev();RemoveData(22); PrintList(); PrintListRev();RemoveData(15); PrintList(); PrintListRev();RemoveData(12); PrintList(); PrintListRev();Free();}