더블 링크드 리스트를 구현 했던 것 같다. 트리는 아니다.
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 117 | #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <Windows.h> #include <string.h> using namespace std; struct Node { char data[20]; Node* next; Node* prv; Node() { memset(data, 0, sizeof(char) * 20); next = nullptr; prv = nullptr; } }; Node* get_Node(const char* str); void remove(Node* head, Node* p); Node* search(Node* head, const char* str); void printList(Node* head); unsigned int countList(Node* head); Node* insert(Node* head, const char* str); int main() { Node* d_Head = new Node; Node* d_Tail = new Node; d_Head->next = d_Tail; d_Tail->prv = d_Head; char find[20]; //count print search insert remove char application[20] = { 0 }; for (int i = 0; i < 10; i++) { cout << "사용할 어플 입력: "; cin >> application; d_Head = insert(d_Head, application); } printf("\n"); cout << "지우려는 데이터를 입력해주세요"; cin >> find; Node* p = search(d_Head, find); remove(d_Head, p); printList(d_Head); printf("\n"); cout << countList(d_Head); return 0; } // Input integer X // Output a pointer to node allocated at Heap memory Node* search(Node* head, const char* str) { if (head->next->next == nullptr) return nullptr; Node* temp = head; while (temp != nullptr) { if (strcmp(temp->data, str) == 0) break; temp = temp->next; } return temp; } unsigned int countList(Node* head) { if (head->next->next == nullptr) return 0; else { Node* temp = head; int count = 0; while (temp) { count++; temp = temp->next; } return count - 2; } } Node* get_Node(const char* str) { Node* pn = nullptr; pn = new Node; strcpy(pn->data, str); return pn; } Node* insert(Node* head, const char* str) { Node* temp = get_Node(str); Node* h_next = head->next; temp->next = h_next; h_next->prv = temp; head->next = temp; temp->prv = head; return head; } void remove(Node* head, Node* p) { if (countList(head) == NULL) { cout << "Is empthy list" << endl; exit(1); } if (p == nullptr || search(head, p->data) == nullptr) { cout << "Could not find data to remove" << endl; exit(1); } else { Node* temp = p->prv; Node* temp2 = p->next; temp->next = temp2; temp2->prv = temp; delete p; } } void printList(Node* head) { int len = countList(head); if (len == NULL) return; Node* temp = head->next; for (int i = 0; i < len; i++) { cout << temp->data << ", "; temp = temp->next; } } | cs |
'자료구조' 카테고리의 다른 글
후위변환과 그 계산. (0) | 2018.05.26 |
---|---|
스택 , 큐, 트리 그리고 깊이탐색과 넓이탐색 (0) | 2018.05.26 |
List 자료구조의 기본적 코드 (0) | 2018.05.26 |
두개의 리스트를 합치는 코드 (0) | 2018.05.26 |
트리 연산과 깊이 및 넓이탐색 (0) | 2018.05.26 |