반응형
< DoubleLLHeader.h >
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #pragma once #include <iostream> using namespace std; typedef int Data; class Node { friend class List; protected: Data data; Node *next; Node *prev; }; class List { private: Node *head; Node *tail; Node *cur; int numOfData = 0; public: // 초기화 List() { head = NULL; tail = NULL; numOfData = 0; } int Count() { return numOfData; } void LInsert(Data data) { Node *newNode = new Node; newNode->data = data; if (head == NULL) { head = newNode; tail = newNode; head->prev = NULL; tail->next = NULL; (numOfData)++; } else { tail->next = newNode; // tail가 가리키는 next는 newNode를 가리키게 한다. newNode->prev = tail; // 그다음 newNode의 prev는 이전 노드인 tail이 가리키던 노드를 가리킨다. tail = newNode; // 그 후 tail은 newNode를 가리키게 한다. newNode->next = NULL; // newNode의 next는 NULL을 가리키고 있다. (numOfData)++; Node *tmp = head; Node *tmp_1 = head->next; while (tmp != tail->next) { int swap; while(tmp_1 != tail->next) { if (tmp->data > tmp_1->data) { swap = tmp->data; tmp->data = tmp_1->data; tmp_1->data = swap; } tmp_1 = tmp_1->next; } tmp = tmp->next; if(tmp != NULL) tmp_1 = tmp->next; } } } int CoutF() { cur = head; if (cur == NULL) { cout << "노드가 존재하지 않습니다." << endl; return 0; } while (cur != tail->next ) { cout << cur->data << " "; cur = cur->next; } cout << endl; return 1; } int CoutB() { cur = tail; if (cur == NULL) { cout << "노드가 존재하지 않습니다." << endl; return 0; } while (cur != head->prev ) { cout << cur->data << " "; cur = cur->prev; } cout << endl; return 1; } int RemoveF() { cur = head; if (cur == NULL) { cout << "노드가 존재하지 않습니다." << endl; return 0; } if(cur->next != NULL) { cur->next->prev = NULL; head = cur->next; delete cur; } else { head = NULL; tail = NULL; delete cur; } numOfData--; return 1; } int RemoveB() { cur = tail; if (cur == NULL) { cout << "노드가 존재하지 않습니다." << endl; return 0; } if (cur->prev != NULL) { cur->prev->next = NULL; tail = cur->prev; delete cur; } else { head = NULL; tail = NULL; delete cur; } numOfData--; return 1; } }; | Crocus |
< Main.cpp >
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 | #include "DoubleLLHeader.h" int main() { List list; int value; int num; while (1) { cout << "현재 노드 개수 :: " << list.Count() << endl; cout << "1. 입력\n2. 앞에서 출력\n3. 뒤에서 출력\n4. 앞의 값 삭제\n5. 뒤의 값 삭제\n6. 종료" << endl; cin >> num; switch(num) { case 1: cout << "값을 입력 하시오 : "; cin >> value; list.LInsert(value); break; case 2: list.CoutF(); break; case 3: list.CoutB(); break; case 4: list.RemoveF(); break; case 5: list.RemoveB(); break; case 6: return 0; } } } | Crocus |
반응형
'Applied > 자료구조' 카테고리의 다른 글
이진 트리 순회(Traversal) (2) (0) | 2016.05.19 |
---|---|
이진 트리 순회(Traversal) (1) (2) | 2016.05.19 |
이중 연결 리스트(Double Linked List) 개념 (0) | 2016.05.11 |
이진 트리(Binary Tree) 개념 (2) - ADT (0) | 2016.05.09 |
이진 트리(Binary Tree) 개념 (1) (0) | 2016.05.09 |