-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList_Class.h
executable file
·326 lines (278 loc) · 5.76 KB
/
List_Class.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#ifndef LIST_CLASS_H
#define LIST_CLASS_H
#include <iostream>
#include <initializer_list>
#include "iterator.h"
template <typename T>
class List
{
private:
friend class iterator<T>;
struct Node
{
T data;
Node* next;
Node* prev;
};
Node* head_;
Node* tail_;
public:
List() :head_(NULL), tail_(NULL){}
List(const T& a);
List(const List& lst);
List(std::initializer_list<T> list);
explicit List(size_t size, const T& value = T());
List& operator =(const List &lst);
~List();
void push_back(const T& a);
void push_front(const T& a);
void pop_back();
void pop_front();
iterator<T> begin()const;
iterator<T> end()const;
iterator<T> insert(iterator<T> pos, const T& value);
iterator<T> erase(iterator<T> pos);
iterator<T> erase(iterator<T> first, iterator<T> last);
std::size_t size();
bool empty()
{
return (head_ == NULL);
}
T& front()
{
return head_->data;
}
const T& front()const
{
return head_->data;
}
T& back()
{
return tail_->data;
}
const T& back()const
{
return tail_->data;
}
};
template <typename T>
List<T>::List(const T& a)
{
head_ = new Node;
head_->data = a;
head_->next = new Node;
head_->next->next = NULL;
head_->next->prev = head_;
head_->next =NULL;
head_->prev = NULL;
tail_ = head_;
}
template <typename T>
List<T>::List(const List<T>& lst)
{
for(iterator<T> i = lst.begin(); i != lst.end(); i++)
{
this->push_back(*i);
}
}
template <typename T>
List<T>::List(std::initializer_list<T> list): head_(NULL), tail_(NULL)
{
for(auto l = list.begin(); l != list.end(); l++)
{
push_back(*l);
}
}
template <typename T>
List<T>::List(size_t size, const T& value): head_(NULL), tail_(NULL)
{
for(std::size_t i = 0; i < size; i++)
{
push_back(value);
}
}
template <typename T>
List<T>& List<T>::operator =(const List<T> &lst)
{
if(&lst != this)
{
this->~List();
for(iterator<T> i = lst.begin(); i != lst.end(); i++)
{
this->push_back(*i);
}
}
return *this;
}
template <typename T>
List<T>::~List()
{
while(head_ != NULL)
{
Node* temp = head_->next;
delete head_;
head_ = temp;
}
}
template <typename T>
void List<T>::push_back(const T& a)
{
Node* temp = new Node;
temp->next = new Node;
temp->next->prev = temp;
temp->next->next = NULL;
temp->data = a;
if (head_ != NULL)
{
delete (tail_->next);
tail_->next = temp;
temp->prev = tail_;
tail_ = temp;
}
else
{
temp->prev = NULL;
head_ = temp;
tail_ = temp;
}
}
template <typename T>
void List<T>::push_front(const T& a)
{
Node* temp = new Node;
temp->prev = NULL;
temp->data = a;
if (head_ != NULL)
{
temp->next = head_;
head_->prev = temp;
head_ = temp;
}
else
{
temp->next = NULL;
head_ = tail_ = temp;
}
}
template <typename T>
void List<T>::pop_back()
{
if(!(this->empty()))
{
if(!(head_ == tail_))
{
Node* temp = tail_->prev;
delete tail_->next;
delete tail_;
tail_ = temp;
tail_->next = new Node;
tail_->next->next = NULL;
tail_->next->prev = tail_;
}
else
{
delete tail_->next;
delete tail_;
tail_ = head_ = NULL;
}
}
}
template <typename T>
void List<T>::pop_front()
{
if(!(this->empty()))
{
if(!(head_ == tail_))
{
Node* temp = head_->next;
delete head_;
head_ = temp;
head_->prev = NULL;
}
else
{
delete tail_;
tail_ = head_ = NULL;
}
}
}
template <typename T>
iterator<T> List<T>::begin()const
{
iterator<T> it(head_);
return it;
}
template <typename T>
iterator<T> List<T>::end()const
{
iterator<T> it(tail_);
return ++it;
}
template <typename T>
iterator<T> List<T>::insert(iterator<T> pos, const T& value)
{
List<T> temp;
iterator<T> ret;
for(iterator<T> p = begin(); p != end(); ++p)
{
if(p == pos)
{
temp.push_back(value);
ret = temp.end();
}
temp.push_back(*p);
}
this->~List();
*this = temp;
return ret;
}
template <typename T>
iterator<T> List<T>::erase(iterator<T> pos)
{
List<T> temp;
iterator<T> ret;
for(iterator<T> p = begin(); p != end(); ++p)
{
if(p == pos)
{
continue;
}
temp.push_back(*p);
ret = temp.end();
}
this->~List();
*this = temp;
return ret;
}
template <typename T>
iterator<T> List<T>::erase(iterator<T> first, iterator<T> last)
{
List<T> temp;
iterator<T> ret;
for(iterator<T> p = begin(); p != end(); ++p)
{
if(p == first)
{
while(p != last + 1)
p++;
}
temp.push_back(*p);
ret = temp.end();
}
this->~List();
*this = temp;
return ret;
}
template <typename T>
std::size_t List<T>::size()
{
std::size_t count = 0;
Node* temp = head_;
while(temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}
#endif // LIST_CLASS_H