-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCList.cpp
More file actions
585 lines (485 loc) · 9.78 KB
/
Copy pathCList.cpp
File metadata and controls
585 lines (485 loc) · 9.78 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// CList CLASS
// 프로그램명 : CList.cpp
// 작 성 자 : NINAEROS.AGAFE
// 작 성 일 : 05/01/25
#include "CList.h"
// Default 생성자
CList::CList()
{
this->head = new NODE; // head 생성
this->tail = new NODE; // tail 생성
// head tail 연결
this->head->Llink = this->head;
this->head->Rlink = this->tail;
this->tail->Llink = this->head;
this->tail->Rlink = this->tail;
this->count = 0;
this->CurrentPosition = this->head;
}
// 복사 생성자
CList::CList(const CList& rCList)
{
NODE* pNode = rCList.head->Rlink;
this->head = new NODE; // head 생성
this->tail = new NODE; // tail 생성
// head tail 연결
this->head->Llink = this->head;
this->head->Rlink = this->tail;
this->tail->Llink = this->head;
this->tail->Rlink = this->tail;
this->count = 0;
this->CurrentPosition = this->head;
while( pNode != rCList.tail )
{
this->InsertTail(pNode->data);
pNode = pNode->Rlink;
}
}
// 소멸자
CList::~CList()
{
this->DeleteAll(); // 요소 전부 삭제
delete this->head; // 머리 삭제
delete this->tail; // 꼬리 삭제
this->count = 0;
}
// 삽입 ( 머리, 꼬리, 값앞, 값뒤, 정렬 )
void CList::InsertHead(const int data)
{
// 삽입할 node 생성
NODE* temp;
temp = new NODE;
temp->data = data;
// node 삽입 연결
temp->Llink = this->head;
temp->Rlink = this->head->Rlink;
this->head->Rlink = temp;
temp->Rlink->Llink = temp;
this->CurrentPosition = temp;
this->count++;
}
void CList::InsertTail(const int data)
{
// 삽입할 node 생성
NODE* temp;
temp = new NODE;
temp->data = data;
// node 삽입 연결
temp->Llink = this->tail->Llink;
temp->Rlink = this->tail;
this->tail->Llink->Rlink = temp;
this->tail->Llink = temp;
this->CurrentPosition = temp;
this->count++;
}
bool CList::InsertBefore(const int key, const int data)
{
bool retVal = false;
NODE* source;
NODE* dest;
dest = this->head->Rlink; // 처음 node
// 머리부터 꼬리까지 반복
while( !retVal && dest != this->tail )
{
// key 와 비교
if( key == dest->data )
{
// 삽입할 node 생성
source = new NODE;
source->data = data;
// 삽입연결
source->Llink = dest->Llink;
source->Rlink = dest;
dest->Llink->Rlink = source;
dest->Llink = source;
this->CurrentPosition = source;
this->count++;
retVal = true;
}
else
{
dest = dest->Rlink;
}
}
return retVal;
}
bool CList::InsertAfter(const int key, const int data)
{
bool retVal = false;
NODE* source;
NODE* dest;
// node 처음
dest = this->head->Rlink;
// 머리부터 꼬리까지 반복
while( !retVal && dest != this->tail )
{
// 비교 key == dest->data
if( key == dest->data )
{
// 삽입할 node 생성
source = new NODE;
source->data = data;
// node 삽입 연결
source->Llink = dest;
source->Rlink = dest->Rlink;
dest->Rlink->Llink = source;
dest->Rlink = source;
this->CurrentPosition =source;
this->count++;
retVal = true;
}
else
{
dest = dest->Rlink;
}
}
return retVal;
}
void CList::InsertSort(const int data)
{
NODE* source;
NODE* dest;
source = new NODE;
source->data = data;
dest = this->head->Rlink;
while( (dest != this->tail) && (data >= dest->data) )
{
dest = dest->Rlink;
}
// 삽입
source->Llink = dest->Llink;
source->Rlink = dest;
dest->Llink->Rlink = source;
dest->Llink = source;
this->CurrentPosition = source;
this->count++;
}
// 삭제 ( 머리, 꼬리, 값앞, 값뒤, 전부, 특정위치 )
bool CList::DeleteHead()
{
bool retVal;
NODE* temp = this->head->Rlink;
// 머리 다음요소 삭제
if( temp == this->tail )
{
retVal = false;
}
else
{
// 삭제할 node를 temp로 삭제
this->head->Rlink = temp->Rlink;
temp->Rlink->Llink = this->head;
temp->Llink = '\0';
temp->Rlink = '\0';
delete temp;
this->CurrentPosition = this->head->Rlink;
this->count--;
retVal = true;
}
return retVal;
}
bool CList::DeleteTail()
{
bool retVal;
NODE* temp;
// 꼬리전 요소 삭제
if( this->tail->Llink == this->head )
{
retVal = false;
}
else
{
// 삭제할 node를 temp로 삭제
temp = this->tail->Llink;
this->tail->Llink = temp->Llink;
temp->Llink->Rlink = this->tail;
temp->Llink = '\0';
temp->Rlink = '\0';
delete temp;
this->CurrentPosition = this->tail->Llink;
this->count--;
retVal = true;
}
return retVal;
}
bool CList::DeleteBefore(const int key)
{
bool retVal = false;
NODE* temp;
NODE* pSource;
temp = this->head->Rlink;
//key값을 찾을때까지 머리에서 꼬리까지 이동
while( (temp != this->tail) && (key != temp->data) )
{
temp = temp->Rlink;
}
// key값인지 아닌지 검사
if( (temp->Llink != this->head) && (key == temp->data) )
{
// key 값 node 삭제
pSource = temp->Llink;
pSource->Rlink->Llink = pSource;
pSource->Llink->Rlink = pSource->Rlink;
this->CurrentPosition = pSource->Rlink;
delete pSource;
this->count--;
retVal = true;
}
return retVal;
}
bool CList::DeleteAfter(const int key)
{
bool retVal = false;
NODE* temp;
temp = this->head->Rlink;
// key값을 찾을때까지 이동
while( (temp != this->tail) && (key != temp->data) )
{
temp = temp->Rlink;
}
if( (key == temp->data) && (temp->Rlink != this->tail) )
{
temp = temp->Rlink;
temp->Rlink->Llink = temp->Llink;
temp->Llink->Rlink = temp->Rlink;
this->CurrentPosition = temp->Llink;
temp->Llink = '\0';
temp->Rlink = '\0';
delete temp;
this->count--;
retVal = true;
}
return retVal;
}
void CList::DeleteAll()
{
// 머리와 꼬리 사이 요소 모두 삭제
while( this->head->Rlink != this->tail )
{
NODE* temp;
temp = this->head->Rlink;
temp->Rlink->Llink = this->head;
this->head->Rlink = temp->Rlink;
delete temp;
}
this->CurrentPosition = this->head;
this->count = 0;
}
bool CList::DeleteAt(const int index)
{
bool retVal = false;
NODE* dest;
dest = this->head; // index만큼이동
for( int i = 0; i < index; i++ )
{
dest = dest->Rlink;
}
if( dest != this->tail )
{
// 삭제 연결
dest->Rlink->Llink = dest->Llink;
dest->Llink->Rlink = dest->Rlink;
delete dest;
this->count--;
retVal = true;
}
return retVal;
}
// 위치이동 ( 전, 후, 처음, 마지막 )
void CList::MoveNext()
{
// 현재위치 다음이 꼬리일 경우
if( this->CurrentPosition->Rlink == this->tail )
{
this->CurrentPosition = this->head;
}
else
{
this->CurrentPosition = this->CurrentPosition->Rlink;
}
}
void CList::MovePrivious()
{
this->CurrentPosition = this->CurrentPosition->Llink;
}
void CList::MoveFirst()
{
// 요소가 없을 경우
if( this->head->Rlink == this->tail )
{
this->CurrentPosition = this->head;
}
else
{
this->CurrentPosition = this->head->Rlink;
}
}
void CList::MoveLast()
{
// 요소가 없을 경우
if( this->head->Rlink == this->tail )
{
this->CurrentPosition = this->head;
}
else
{
this->CurrentPosition = this->tail->Llink;
}
}
// 읽기 쓰기
int CList::GetNode(const int index)
{
NODE* dest;
dest = this->head;
// index 만큼이동
for( int i = 0; i < index; i++ )
{
dest = dest->Rlink;
}
return dest->data;
}
bool CList::SetNode(const int key, const int data)
{
bool retVal = false;
NODE* dest;
dest = this->head->Rlink;
// key갑을 찾을때까지 이동
while( (dest != this->tail) && (key != dest->data) )
{
dest = dest->Rlink;
}
// key 값을 찾았는지 검사
if( key == dest->data )
{
dest->data = data;
retVal = true;
this->CurrentPosition = dest;
}
return retVal;
}
// 검색
int CList::SearchNode(const int key)
{
int index;
int count = 0;
NODE* dest;
dest = this->head->Rlink;
index = -1;
// key값 위치 찾기
while( (dest != this->tail) && (key != dest->data) )
{
dest = dest->Rlink;
count++;
}
if( key == dest->data )
{
index = count;
}
return index;
}
// 요소 개수 얻기
int CList::GetCount() const
{
return this->count;
}
// 요소 출력
void CList::PrintList()
{
NODE* temp;
temp = this->head->Rlink;
while( temp != this->tail )
{
cout << temp->data << " ";
temp = temp->Rlink;
}
cout << endl;
}
// 연산자 재정의
// CList = CList
CList& CList::operator = (CList& rCList)
{
NODE* pNode = rCList.head->Rlink;
this->head = new NODE; // head 생성
this->tail = new NODE; // tail 생성
// head tail 연결
this->head->Llink = this->head;
this->head->Rlink = this->tail;
this->tail->Llink = this->head;
this->tail->Rlink = this->tail;
this->count = 0;
this->CurrentPosition = this->head;
while( pNode != rCList.tail )
{
this->InsertTail(pNode->data);
pNode = pNode->Rlink;
}
return *this;
}
// CList == CList
bool CList::operator == (CList& rCList)
{
bool retVal = false;
if(this->count == rCList.count)
{
int true_count = 1;
NODE* pSource = this->head->Rlink;
NODE* pDest = rCList.head->Rlink;
while( true_count != 0 && pSource != this->tail )
{
if( pSource->data == pDest->data )
{
true_count *= true_count;
}
else
{
true_count = 0;
}
}
if( true_count )
{
retVal = true;
}
}
return retVal;
}
// CList + CList
CList& operator + (CList& rLeft, CList& rRight)
{
CList* temp;
temp = new CList;
NODE* pLeftList = rLeft.head->Rlink;
NODE* pRightList = rRight.head->Rlink;
while( pLeftList != rLeft.tail )
{
temp->InsertTail(pLeftList->data);
pLeftList = pLeftList->Rlink;
}
while( pRightList != rRight.tail )
{
temp->InsertTail(pRightList->data);
pRightList = pRightList->Rlink;
}
return *temp;
}
// CList += CList
CList& CList::operator += (CList& rCList)
{
NODE* pRight = rCList.head->Rlink;
while( pRight != rCList.tail )
{
this->InsertTail(pRight->data);
pRight = pRight->Rlink;
}
return *this;
}
// cin <<
ostream& operator << (ostream& Output, CList& rCList)
{
NODE* pSource = rCList.head->Rlink;
while( pSource != rCList.tail )
{
Output << pSource->data << " ";
pSource = pSource->Rlink;
}
return Output;
}