Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자료구조
- WinAPI
- Visual Micro
- 라인트레이서
- 수광 소자
- Array
- directx
- 아두이노 소스
- 운영체제
- 아두이노 컴파일러
- vector
- Stack
- map
- list
- stl
- html
- queue
- 시스템프로그래밍
- C언어
- LineTracer
- arduino compiler
- 아두이노
- Algorithm
- 컴퓨터 그래픽스
- Arduino
- 통계학
- c++
- set
- priority_queue
- Deque
Archives
- Today
- Total
Kim's Programming
[C++/템플릿으로 만드는 자료구조] 이중연결원형리스트(Doubly Circular Linked List) 본문
Programming/Data Structure
[C++/템플릿으로 만드는 자료구조] 이중연결원형리스트(Doubly Circular Linked List)
Programmer. 2016. 1. 18. 21:06이중연결원형 리스트의 템플릿화된 소스입니다. 사용시엔 Iostream을 인클루드 시켜야합니다.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | template<typename Type> class Node { public: Type VALUE; Node<Type> *Before; Node<Type> *After; }; template<typename Type> class List { private: Node<Type> *header = NULL; Node<Type> *tailer = NULL; public: List(); ~List(); void Add_First(Type Item); void Add(int Position, Type Item); void Push_Back(Type Item); Type Delete(int Position); void Replace(int Position, Type Item); void Find(Type Item); void Find_R(Type item); void Get_Entry(int Position); void Get_Entry_R(int Position); void Get_Length(); bool Is_Empty(); void Printing(); void Printing_R(); }; template<typename Type> List<Type>::List() { this->header = NULL; this->tailer = NULL; } template<typename Type> List<Type>::~List() { } template<typename Type> void List<Type>::Add_First(Type Item) { Node<Type> *New = new Node<Type>; New->VALUE = Item; if (this->header == NULL)// 리스트가 비었을 때의 경우 { this->tailer = New; this->tailer->After = New; this->tailer->Before = New; New->After = this->tailer; New->Before = this->tailer; this->header = New; return; } if (this->header != NULL)//리스트에 값이 있을 경우 { New->Before = this->header->Before; this->tailer->After = New; New->After = this->header; this->header->Before = New; this->header = New; return; } } template<typename Type> void List<Type>::Add(int Position, Type Item) { if (Position == 1 || this->header == NULL || this->tailer == NULL) //1번째의 경우와 그 이외의 경우의 연산 차이가 있으므로 1번 쨰 경우를 분리 { this->Add_First(Item); return; } Node<Type> *Cur = this->header; Node<Type> *Temp = NULL; Node<Type> *New = new Node<Type>; int pos = 1; //위치 한계값 설정을 위해 지정 if (this->header == this->tailer) { this->header->After = New; this->header->Before = New; New->Before = this->header; New->After = this->header; this->tailer = New; New->VALUE = Item; return; } if (Position == 2) //시작시에 커서 이동을 해야하나 이전 것을 체크해줘야 하기 때문에 2번쨰와 그 이후를 분리 { Temp = Cur; Cur = Cur->After; New->VALUE = Item; New->Before = Cur->Before; New->After = Temp->After; Temp->After = New; New->After = Cur; return; } if (Position == 3 && (this->header->After == this->tailer)) { tailer->After = New; New->Before = this->tailer; New->After = this->header; this->header->Before = New; New->VALUE = Item; this->tailer = New; return; } if (Position == 3 && (this->header->After->After == this->tailer)) { New->Before = this->tailer->Before; New->After = New->Before->After; New->Before->After = New; this->tailer->Before = New; New->VALUE = Item; return; } if (Position == 3) { New->After = this->header->After->After; New->Before = this->header->After; this->header->After->After = New; New->After->Before = New; New->VALUE = Item; return; } Cur = Cur->After; pos++; while (pos != Position - 1) { if (Cur->After == this->header) { std::cout << "Cannot Create Node in" << Position << "(th) Node" << std::endl; return; } pos++; Cur = Cur->After; } if (Cur == this->header) { New->After = this->header; New->Before = this->tailer; this->tailer->After = New; this->header->Before = New; New->VALUE = Item; return; } New->VALUE = Item; New->After = Cur->After; New->Before = Cur; New->After->Before = New; Cur->After = New; } template<typename Type> void List<Type>::Push_Back(Type Item) { Node<Type> *New = new Node<Type>; Node<Type> *Cur = this->tailer; if (this->header == NULL) { Add_First(Item); return; } New->After = this->tailer->After; New->Before = this->header->Before; this->header->Before = New; this->tailer->After = New; New->VALUE = Item; } template<typename Type> Type List<Type>::Delete(int Position) { int pos = 1; int Return_Value; Node<Type> *Temp = NULL; Node<Type> *Cur = this->header; while (pos < Position) { if (pos == Position - 1) Temp = Cur; Cur = Cur->After; pos++; } Return_Value = Cur->VALUE; Cur->After->Before = Cur->Before; Cur->Before->After = Cur->After; delete(Cur); return Return_Value; } template<typename Type> void List<Type>::Replace(int Position, Type Item) { int pos = 1; Node<Type> *Cur = this->header; while (pos < Position) { Cur = Cur->After; pos++; } Cur->VALUE = Item; } template<typename Type> void List<Type>::Find(Type Item) { Node<Type> *Cur = this->header; int i = 1; if (Cur->VALUE == Item) std::cout << " Value" << Item << " in " << i << " (th) node" << std::endl; i++; Cur = Cur->After; while (Cur != this->header) { if (Cur->VALUE == Item) std::cout << " Value" << Item << " in " << i << " (th) node" << std::endl; i++; Cur = Cur->After; } if (Cur == NULL) std::cout << Item << " is not in this List" << std::endl; } template<typename Type> void List<Type>::Find_R(Type Item) { Node<Type> *Cur = this->tailer; int i = 1; if (Cur->VALUE == Item) std::cout << "Value " << Item << " is in " << i << " (th) Node" << std::endl; i++; Cur = Cur->Before; while (Cur != this->header) { if (Cur->VALUE == Item) std::cout << "Value " << Item << " is in " << i << " (th) Node" << std::endl; i++; Cur = Cur->Before; } } template<typename Type> void List<Type>::Get_Entry(int Position) { int pos = 1; Node<Type> *Cur = this->header; if (Position == 1) { std::cout << Cur->VALUE << " is in " << Position << "(th) Node" << std::endl; return; } Cur = Cur->After; pos++; while (pos < Position) { if (Cur == NULL) { std::cout << "No Node" << std::endl; return; } Cur = Cur->After; pos++; } std::cout << Cur->VALUE << " is in " << Position << "(th) Node" << std::endl; } template<typename Type> void List<Type>::Get_Entry_R(int Position) { int pos = 1; Node<Type> *Cur = this->header; if (Position == 1) { std::cout << "(Reverse) " << Cur->VALUE << " is in " << Position << "(th) Node" << std::endl; return; } Cur = Cur->Before; pos++; while (pos < Position) { if (Cur == NULL) { std::cout << "No Node" << std::endl; return; } Cur = Cur->Before; pos++; } std::cout << "(Reverse) " << Cur->VALUE << " is in " << Position << "(th) Node" << std::endl; } template<typename Type> void List<Type>::Get_Length() { Node<Type> *Cur = this->header; int i = 1; i++; Cur = Cur->After; while (Cur != this->header) { i++; Cur = Cur->After; } std::cout << "This List has " << i << "nodes" << std::endl; } template<typename Type> bool List<Type>::Is_Empty() { if (this->header == NULL) { std::cout << "List is empty" << std::endl; return true; } std::cout << "List is not empty" << std::endl; return false; } template<typename Type> void List<Type>::Printing() { if (this->Is_Empty()) { std::cout << "There is No Node to display" << std::endl; return; } Node<Type> *Cur = this->header; std::cout << Cur->VALUE << " -> "; Cur = Cur->After; while (Cur != this->header) { std::cout << Cur->VALUE << " -> "; Cur = Cur->After; } std::cout << std::endl; } template<typename Type> void List<Type>::Printing_R() { if (this->Is_Empty()) { std::cout << "There is No Node to display" << std::endl; return; } Node<Type> *Cur = this; std::cout << Cur->VALUE << " -> "; Cur = Cur->Before; while (Cur != this->header) { std::cout << Cur->VALUE << " -> "; Cur = Cur->Before; } std::cout << std::endl; } | cs |
이중연결원형리스트는 STL에서 List 헤더를 이용하여 이용하게됩니다.
같이보기
'Programming > Data Structure' 카테고리의 다른 글
[C++/템플릿으로 만드는 자료구조] 큐(Queue) (0) | 2016.01.18 |
---|---|
[C++/템플릿으로 만드는 자료구조] 스택(Stack) (0) | 2016.01.18 |
[C++/템플릿으로 만드는 자료구조] 단순원형연결리스트(Circular Linked List) (0) | 2016.01.18 |
[C++/템플릿으로 만드는 자료구조] 단순연결리스트(Linked List) (1) | 2016.01.18 |
[C로 만드는 자료구조]이진 탐색 트리(Binary Search Tree) (16) | 2015.12.25 |