-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.cpp
301 lines (225 loc) · 5.76 KB
/
list.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
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
#include <iostream>
#include <string>
using namespace std;
class User
{
public:
enum Group { ADMIN, MODERATOR, USER, BANNED };
private:
string username;
string email;
Group group;
public:
User (string username_, string email_, Group group_) : username(username_), email(email_), group(group_) {}
User (string username_) : username(username_), group(BANNED) {}
User (const User& original) : username(original.username), email(original.email), group(original.group) {}
string getUsername () const { return username; }
bool equal (const User& right) const
{
return username == right.username;
}
void post (string msg) const
{
if ( group == BANNED ) return;
cout << "Message left from: " << username << endl;
cout << msg << endl;
cout << "Contact me at: " << email << endl;
}
};
typedef User Data;
//typedef int Data;
class List
{
struct Node
{
Data data;
Node* next;
Node (Data data_, Node* next_ = NULL) : data(data_), next(next_) {}
};
Node* first;
Node* last;
unsigned int nodes;
public:
class Iterator
{
Node* node;
public:
Iterator (Node* node_) : node(node_) {}
Iterator next (int amount = 1)
{
for ( int i = 0 ; i < amount ; i++ ) node = node->next;
return Iterator (node);
}
bool equal (Iterator right)
{
return node == right.node;
}
Data& data () { return node->data; }
Node* container () { return node; }
};
// Iterator at the beginning of the list
Iterator begin () { return Iterator (first); }
// Iterator *after* the end of the list
Iterator end () { return Iterator (NULL); }
Iterator insert (Data data_, bool atEnd = true)
{
nodes++;
Node* tmp;
if ( atEnd )
{
tmp = new Node (data_);
// list is empty
if ( last == NULL )
{
first = tmp;
last = tmp;
}
else
{
last->next = tmp;
last = last->next;
}
}
else
{
tmp = new Node (data_, first);
// list is empty
if ( first == NULL )
{
first = tmp;
last = tmp;
}
else
first = tmp;
}
return Iterator (tmp);
}
// Insert after position
Iterator insert (Data data_, Iterator position)
{
Node* current = position.container();
if ( current == NULL ) return insert (data_, false);
Node* tmp = new Node (data_, current->next);
// last node
if ( current->next == NULL ) last = tmp;
current->next = tmp;
nodes++;
return position.next();
}
Iterator find (Data data_, Iterator after = Iterator(NULL))
{
Node* afterPos = after.container();
Node* tmp = (afterPos == NULL ? first : afterPos->next);
while ( tmp != NULL )
{
//if ( tmp->data == data_ ) break;
if ( tmp->data.equal(data_) ) break;
tmp = tmp->next;
}
return Iterator (tmp);
}
void remove (Data data_, Iterator after = Iterator(NULL))
{
Node* afterPos = after.container();
Node* tmp = (afterPos == NULL ? first : afterPos->next);
Node* prev = (afterPos == NULL ? NULL : afterPos);
while ( tmp != NULL )
{
//if ( tmp->data == data_ )
if ( tmp->data.equal(data_) )
{
// update first
if ( prev == NULL) first = first->next;
else prev->next = tmp->next;
// update last
if ( tmp->next == NULL ) last = prev;
nodes--;
delete tmp;
break;
}
prev = tmp;
tmp = tmp->next;
}
}
void update (Data data_, Iterator pos)
{
pos.data() = data_;
}
unsigned int size () { return nodes; }
List () : first(NULL), last(first), nodes(0) {}
List (Iterator& itStart, Iterator& itEnd) : first(NULL), last(first), nodes(0)
{
for ( ; !itStart.equal( itEnd ) ; itStart.next() )
insert ( itStart.data() );
}
List (const List& original)
{
for ( Node* tmp = original.first ; tmp != NULL ; tmp = tmp->next )
insert ( tmp->data );
}
~List ()
{
Node *temp, *i = first;
while (i != NULL)
{
temp = i;
i = i->next;
delete temp;
}
}
};
/* =========================================================================== */
int main ()
{
List list;
list.insert (User("george", "[email protected]", User::ADMIN));
list.insert (User("sdi1200321", "[email protected]", User::USER));
list.insert (User("testUser", "[email protected]", User::USER));
list.insert (User("Vicky", "[email protected]", User::MODERATOR));
list.insert (User("errikos", "[email protected]", User::MODERATOR));
List::Iterator it = list.begin();
it.next(); it.next();
it = list.insert (User("stef", "[email protected]", User::ADMIN), it);
list.insert (User("sdi1200333", "[email protected]", User::USER), it);
for ( it = list.begin() ; !it.equal( list.end() ) ; it.next() )
cout << it.data().getUsername() << endl;
it = list.find (User("george"));
if ( !it.equal( list.end() ) ) cout << "USER FOUND!" << endl;
else cout << "USER NOT FOUND!" << endl;
list.remove (User("testUser"), it);
list.update (User("sdi1100400", "[email protected]", User::USER), it);
for ( it = list.begin() ; !it.equal( list.end() ) ; it.next() )
cout << it.data().getUsername() << endl;
User u("ge", "asdas", User::USER);
u.post("SADASDAS");
const User l("ge", "sadasd", User::MODERATOR);
l.post("AAAA");
if ( u.equal(l) ) cout << "A" << endl;
if ( l.equal(u) ) cout << "B" << endl;
/*
List list;
list.insert (1);
list.insert (2);
list.insert (10);
list.insert (4);
list.insert (2);
List::Iterator it = list.begin();
it.next(); it.next();
it = list.insert (90, it);
list.insert (2, it);
for ( it = list.begin() ; !it.equal( list.end() ) ; it.next() )
cout << it.data() << endl;
it = list.find (15);
if ( !it.equal( list.end() ) ) cout << "15 FOUND!" << endl;
else cout << "15 NOT FOUND!" << endl;
it = list.find (10);
if ( !it.equal( list.end() ) ) cout << "10 FOUND!" << endl;
else cout << "10 NOT FOUND!" << endl;
it = list.find (2);
it = list.find (2, it);
list.remove (2, it);
list.update (123, it);
for ( it = list.begin() ; !it.equal( list.end() ) ; it.next() )
cout << it.data() << endl;
*/
}