티스토리 뷰

 윤성우의 열혈자료구조 C로 된 연결리스트의 정렬 삽입의 구현을 C++ 형식으로 나름대로 바꿔보았다.
연결리스트의 전반적인 설명보다. c++형식으로 바꾼다음 기록을 목적으로 코드를 올림.


(#현재 정렬기능이 이상한데 차후에 봐야겟다. 스택까지의 진도에서 필요한건 정렬이 아니니깐 우선 연결리스트의 구현만 올리겠음)



Node.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef NODE_H
#define NODE_H
 
typedef int LData;
 
class Node
{
private:
    LData data;
public:
 
    Node *next;
 
    Node(){}
    Node(LData data):data(data){}
 
    LData getdata()
    {
        return data;
    }
};
#endif // !NODE_H

cs



LinkedList.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
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
 
#include"Node.h"
 
class LinkedList
{
private:
    Node *head;
    Node *cur;
    Node *before;
    int numOfData;
    int(*comp)(LData d1, LData d2);
 
public:
    LinkedList();
    void LInsert(LData data);
    void FInsert(LData data);
    void SInsert(LData data);
 
    LData LRemove();
    bool LFirst(LData *pdata);
    bool LNext(LData *pdata);
    int LCount();
 
    void SetSortRule(int(*comp)(LData d1, LData d2));
};
#endif // !LINKEDLIST_H



cs



LinkedList.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
#include "LinkedList.h"
#include <iostream>
using namespace std;
 
LinkedList::LinkedList()
{
    Node *newNode = new Node;
    head = newNode;
    newNode->next = nullptr;
    comp = nullptr;
    numOfData = 0;
    cout << "Dummy head->next:" << head->next << endl;
}
 
void LinkedList::FInsert(LData data)
{
    Node *newNode = new Node(data);
    newNode->next = head->next;
    head->next = newNode;
    
    numOfData++;
    cout <<"head : "<<head<< ", head->next:" << head->next << ", numOfData: " << numOfData << endl;
}
 
void LinkedList::SInsert(LData data)
{
    Node *newNode = new Node(data);
    Node *pred = head;
    
    while (pred->next != nullptr && comp(data, pred->getdata()) != 0)
    {
        pred = pred->next;
    }
 
    newNode->next = pred->next;
    pred->next = newNode;
    numOfData++;
}
 
void LinkedList::LInsert(LData data)
{
    if (comp == nullptr)
        FInsert(data);
    else
        SInsert(data);
}
 
bool LinkedList::LFirst(LData *pdata)
{
    if (head->next == nullptr)
        return false;
 
     before = head;
     cout << "LinkedList head: " << head << endl;
     cout << "LinkedList head->next: " << head->next << endl;
      cur = head->next;
     *pdata = cur->getdata();
 
     return true;
}
 
bool LinkedList::LNext(LData *pdata)
{
    if (cur->next == nullptr)
        return false;
 
    before = cur;
    cur = cur->next;
    *pdata = cur->getdata();
 
    return true;
}
 
LData LinkedList::LRemove()
{
    Node *rpos = cur;
    LData rdata = cur->getdata();
 
    before->next = cur->next;
    cur = before;
 
    delete rpos;
    numOfData--;
 
    return rdata;
}
 
int LinkedList::LCount()
{
    return numOfData;
}
 
void LinkedList::SetSortRule(int(*comp)(LData d1, LData d2))
{
    this->comp = comp;
}
 
cs



LinkedListMain.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
#include"LinkedList.h"
#include <iostream>
 
using namespace std;
 
int WhoIsPrecede(int d1, int d2)
{
    if (d1 > d2)
        return 0;
    else
        return 1;
}
 
int main()
{
    LinkedList List;
    int data;
 
    List.LInsert(22);
    List.LInsert(11);
    List.LInsert(33);
    List.LInsert(55);
    List.LInsert(44);
 
    List.SetSortRule(WhoIsPrecede);
 
    cout << "현재의 데이터수 : " << List.LCount() << endl;
    
    if (List.LFirst(&data))
    {
        cout << data<<" " ;
 
        while (List.LNext(&data))
            cout << data <<" ";
    }
 
    if (List.LFirst(&data))
    {
        cout << "###데이터삭제###" << endl;
        if (data == 22)
            List.LRemove();
 
        while (List.LNext(&data))
        {
            if (data == 22)
                List.LRemove();
        }
    }
 
    cout << "현재의 데이터수 : " << List.LCount() << endl;
    if (List.LFirst(&data))
    {
        cout << data << " ";
 
        while (List.LNext(&data))
            cout << data << " ";
    }
    return 0;
}
cs


Comments