티스토리 뷰

달라진건 거의 없다.

Node.h는 바뀐게 없음


CLinkedList.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
#ifndef CLINKEDLIST_H
#define CLINKEDLIST_H
 
#include"Node.h"
 
class CLinkedList
{
private:
    Node *tail;
    Node *cur;
    Node *before;
    int numOfData;
 
public:
    CLinkedList();
    void LInsert(LData data); //꼬리에 노드를 추가
    void LInsertFront(LData data); //머리에 노드를 추가
 
    bool LFirst(LData *pdata);
    bool LNext(LData *pdata);
    LData LRemove();
    int LCount();
};
#endif // !CLINKEDLIST_H

cs



CLinkedList.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
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
#include "CLinkedList.h"
#include <iostream>
using namespace std;
 
CLinkedList::CLinkedList()
{
    //Node *newNode = new Node; 더미노드 없음.
    tail = nullptr;
    cur = nullptr;
    before = nullptr;
    numOfData = 0;
}
 
void CLinkedList::LInsert(LData data)
{
    Node *newNode = new Node(data);
        
    if (tail == nullptr)
    {
        tail = newNode;
        tail->next = newNode;
    }
    else
    {
        newNode->next = tail->next;
        tail->next = newNode;
        tail = newNode;
    }
    numOfData++;
}
 
void CLinkedList::LInsertFront(LData data)
{
    Node *newNode = new Node(data);
 
    if (tail == nullptr)
    {
        tail = newNode;
        tail->next = newNode;
    }
    else
    {
        newNode->next = tail->next;
        tail->next = newNode;
    }
    numOfData++;
}
 
bool CLinkedList::LFirst(LData *pdata)
{
    if (tail->next == nullptr)
        return false;
 
     before = tail;
     cout << "LinkedList head: " << tail << endl;
     cout << "LinkedList head->next: " << tail->next << endl;
      cur = tail->next;
     *pdata = cur->getdata();
 
     return true;
}
 
bool CLinkedList::LNext(LData *pdata)
{
    if (tail == nullptr)
        return false;
 
    before = cur;
    cur = cur->next;
    *pdata = cur->getdata();
 
    return true;
}
 
LData CLinkedList::LRemove()
{
    Node *rpos = cur;
    LData rdata = rpos->getdata();
 
    if (rpos == tail)
    {
        if (tail == nullptr)
            tail = nullptr;
        else
            tail = before;
    }
 
    before->next = cur->next;
    cur = before;
 
    delete rpos;
    numOfData--;
    return rdata;
}
 
int CLinkedList::LCount()
{
    return numOfData;
}
cs


Comments