-
Notifications
You must be signed in to change notification settings - Fork 31
/
Activity01.cpp
215 lines (183 loc) · 3.12 KB
/
Activity01.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
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
#include <iostream>
template <typename T>
struct cir_list_node
{
T* data;
cir_list_node* next;
cir_list_node* prev;
~cir_list_node()
{
delete data;
}
};
template <typename T>
struct cir_list
{
public:
using node = cir_list_node<T>;
using node_ptr = node*;
private:
node_ptr head;
size_t n;
public:
cir_list() : n(0)
{
head = new node {NULL, NULL, NULL}; // 모두 NULL로 구성된 기본 노드
head->next = head;
head->prev = head;
}
size_t size() const
{
return n;
}
void insert(const T& value)
{
node_ptr newNode = new node {new T(value), NULL, NULL};
n++;
auto dummy = head->prev;
dummy->next = newNode;
newNode->prev = dummy;
if (head == dummy)
{
dummy->prev = newNode;
newNode->next = dummy;
head = newNode;
return;
}
newNode->next = head;
head->prev = newNode;
head = newNode;
}
void erase(const T& value)
{
auto cur = head, dummy = head->prev;
while (cur != dummy)
{
if (*(cur->data) == value)
{
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
if (cur == head)
head = head->next;
delete cur;
n--;
return;
}
cur = cur->next;
}
}
struct cir_list_it
{
private:
node_ptr ptr;
public:
cir_list_it(node_ptr p) : ptr(p) {}
T& operator*()
{
return *(ptr->data);
}
node_ptr get()
{
return ptr;
}
cir_list_it& operator++()
{
ptr = ptr->next;
return *this;
}
cir_list_it operator++(int)
{
cir_list_it it = *this;
++(*this);
return it;
}
cir_list_it& operator--()
{
ptr = ptr->prev;
return *this;
}
cir_list_it operator--(int)
{
cir_list_it it = *this;
--(*this);
return it;
}
friend bool operator==(const cir_list_it& it1, const cir_list_it& it2)
{
return it1.ptr == it2.ptr;
}
friend bool operator!=(const cir_list_it& it1, const cir_list_it& it2)
{
return it1.ptr != it2.ptr;
}
};
cir_list_it begin()
{
return cir_list_it {head};
}
cir_list_it begin() const
{
return cir_list_it {head};
}
cir_list_it end()
{
return cir_list_it {head->prev};
}
cir_list_it end() const
{
return cir_list_it {head->prev};
}
cir_list(const cir_list<T>& other) : cir_list()
{
// 아래 코드는 원소를 역순으로 삽입하지만,
// 원형 리스트이기 때문에 문제가 없습니다.
for (const auto& i : other)
insert(i);
}
cir_list(const std::initializer_list<T>& il) : head(NULL), n(0)
{
// 아래 코드는 원소를 역순으로 삽입하지만,
// 원형 리스트이기 때문에 문제가 없습니다.
for (const auto& i : il)
insert(i);
}
~cir_list()
{
while (size())
{
erase(*(head->data));
}
delete head;
}
};
struct playlist
{
cir_list<int> list;
void insert(int song)
{
list.insert(song);
}
void erase(int song)
{
list.erase(song);
}
void loopOnce()
{
for (auto& song : list)
std::cout << song << " ";
std::cout << std::endl;
}
};
int main()
{
playlist pl;
pl.insert(1);
pl.insert(2);
std::cout << "재생 목록 : ";
pl.loopOnce();
playlist pl2 = pl;
pl2.erase(2);
pl2.insert(3);
std::cout << "두 번째 재생 목록 : ";
pl2.loopOnce();
}