반응형
더미 노드를 이용한 이중 링크드 리스트 자료구조를 기반으로 deque를 구현
http://www.crocus.co.kr/1180?category=150836
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | #include <iostream> #include <cstdio> using namespace std; class NODE{ friend class DEQUE; private: int data; NODE *left; NODE *right; }; class DEQUE { private: int sz; NODE *head; NODE *tail; public: DEQUE(){ //cout << "\n\n생성자 호출\n\n" << endl; sz = 0; NODE *dummyHead = new NODE; NODE *dummyTail = new NODE; dummyHead->left = dummyHead->right = NULL; dummyTail->left = dummyTail->right = NULL; head = dummyHead; tail = dummyTail; } ~DEQUE() { init(); delete head; delete tail; //cout << "\n\n소멸자 호출\n\n " << endl; } void init() { // 더미노드 사이 모든 값 삭제 NODE *pos = head; while (pos->right != NULL) { NODE *delNode = pos; pos = pos->right; delete delNode; } delete tail; // 마지막 tail이 삭제되지 않았다. // 더미노드 새로 생성(생성자와 동일) NODE *dummyHead = new NODE; NODE *dummyTail = new NODE; dummyHead->left = dummyHead->right = NULL; dummyTail->left = dummyTail->right = NULL; head = dummyHead; tail = dummyTail; sz = 0; } void push_front(int val){ if (head->right == NULL) { NODE *newNode = new NODE; newNode->data = val; newNode->left = head; head->right = newNode; newNode->right = tail; tail->left = newNode; } else { NODE *newNode = new NODE; newNode->data = val; newNode->right = head->right; head->right->left = newNode; newNode->left = head; head->right = newNode; } sz++; } void push_back(int val) { if (head->right == NULL) { NODE *newNode = new NODE; newNode->data = val; newNode->left = head; head->right = newNode; newNode->right = tail; tail->left = newNode; } else { NODE *newNode = new NODE; newNode->data = val; newNode->left = tail->left; tail->left->right = newNode; newNode->right = tail; tail->left = newNode; } sz++; } void pop_front() { if (!sz) cout << "deque가 비어있습니다." << endl; else { NODE *pos = head->right; head->right->right->left = head; head->right = head->right->right; delete pos; sz--; } } void pop_back() { if (!sz) cout << "deque가 비어있습니다." << endl; else { NODE *pos = tail->left; tail->left->left->right = tail; tail->left = tail->left->left; delete pos; sz--; } } int front() { if (!sz) cout << "deque가 비어있습니다." << endl; else return head->right->data; } int back() { if (!sz) cout << "deque가 비어있습니다." << endl; else return tail->left->data; } void all() { NODE *pos = head; while (pos != NULL) { // 더미노드는 출력하지 않는다. if (!(pos == head || pos == tail)) cout << pos->data << " "; pos = pos->right; } cout << endl; } int size() { return sz; } bool empty() { return !sz; } }; void menu() { cout << "┏━━━━━━━━━━━━━━━━┓" << endl; cout << "┃ 1 :: init ┃" << endl; cout << "┃ 2 :: push_front┃" << endl; cout << "┃ 3 :: push_back ┃" << endl; cout << "┃ 4 :: pop_front ┃" << endl; cout << "┃ 5 :: pop_back ┃" << endl; cout << "┃ 6 :: front ┃" << endl; cout << "┃ 7 :: back ┃" << endl; cout << "┃ 8 :: all ┃" << endl; cout << "┃ 9 :: size ┃" << endl; cout << "┃ 10 :: empty ┃" << endl; cout << "┃ 11 :: exit ┃" << endl; cout << "┗━━━━━━━━━━━━━━━━┛" << endl; } int main() { DEQUE dq; cout << "Datastructure :: Deque" << endl; while (1) { menu(); int num; cin >> num; if (num == 1) dq.init(); else if (num == 2) { int val; cout << "값 :: "; cin >> val; dq.push_front(val); } else if (num == 3) { int val; cout << "값 :: "; cin >> val; dq.push_back(val); } else if (num == 4) dq.pop_front(); else if (num == 5) dq.pop_back(); else if (num == 6) cout << dq.front() << endl; else if (num == 7) cout << dq.back() << endl; else if (num == 8) dq.all(); else if (num == 9) cout << dq.size() << endl; else if (num == 10) cout << (dq.empty() ? "empty" : "non empty") << endl; else if (num == 11) break; cout << endl; } return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 자료구조' 카테고리의 다른 글
재귀가 아닌 반복문을 이용한 Trie (0) | 2018.03.07 |
---|---|
힙(Heap) 자료구조 (4) | 2018.03.04 |
[STL] list 구현 (2) | 2018.02.28 |
[STL] Pair 구현 (0) | 2018.01.26 |
[STL] Vector 구현 (0) | 2018.01.26 |