-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic_list.cpp
More file actions
197 lines (176 loc) · 3.62 KB
/
music_list.cpp
File metadata and controls
197 lines (176 loc) · 3.62 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
// 원형음악 플레이어//
#include<iostream>
#include<algorithm>
template<typename T>
struct Music
{
T* data;
Music* prev;
Music* next;
~Music()
{
delete data;
}
};
template<typename T>
class Circle_Music{
public:
using node=Music<T>;
using node_ptr=node*;
private:
node_ptr head;
size_t n;
public:
Circle_Music():n(0)
{
head=new node{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(dummy==head)
{
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;
n--;
return;
}
cur=cur->next;
}
}
struct music_list
{
private:
node_ptr ptr;
public:
music_list(node_ptr p):ptr(p){}
T& operator*()
{
return *(ptr->data);
}
node_ptr get(){
return ptr;
}
music_list& operator++()
{
ptr=ptr->next;
return *this;
}
music_list operator++(int)const{
music_list result=*this;
++(*this);
return result;
}
music_list& operator--()
{
ptr=ptr->prev;
return *this;
}
music_list operator--(int)const
{
music_list result=*this;
--(*this);
return result;
}
friend bool operator==(const music_list& left,const music_list&right)
{
return left.ptr==right.ptr;
}
friend bool operator!=(const music_list& left, const music_list&right)
{
return left.ptr!=right.ptr;
}
};
music_list begin()
{
return music_list{head};
}
music_list begin()const
{
return music_list{head};
}
music_list end()
{
return music_list{head->prev};
}
music_list end() const
{
return music_list{head->prev};
}
Circle_Music(const Circle_Music<T>& other):Circle_Music()
{
for(const auto &i: other)
insert(i);
}
Circle_Music(const std::initializer_list<T>&il):head(NULL),n(0){
for(const auto &i:il)
insert(i);
}
~Circle_Music()
{
while(size())
{
erase(*(head->data));
}
delete head;
}
};
template<typename T>
struct playlist
{
Circle_Music<T> list;
void insert(T song)
{
list.insert(song);
}
void erase(T song)
{
list.erase(song);
}
void loopOnce()
{
for(auto& song: list)
std::cout<<song<<" ";
std::cout<<std::endl;
}
};
int main()
{
playlist<std::string> p1;
p1.insert("power");
p1.insert("over");
std::cout<<"music_List: ";
p1.loopOnce();
playlist<std::string> p12=p1;
p12.erase("over");
p12.insert("name");
std::cout<<"Music_List: ";
p12.loopOnce();
}