-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList2.h
304 lines (254 loc) · 4.78 KB
/
List2.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
#ifndef List2_h
#define List2_h
#include <iostream>
using namespace std;
template <typename T> class List2{
public:
List2();//crt
~List2();//dsry
List2(const List2&);//cpy
List2& operator = (const List2&);//=
bool operator == (const List2&)const;
bool operator != (const List2&)const;
friend ostream& operator << (ostream&,const List2&);
void addToBegin(const T);
void add(const T);
void makeItEmpty();
void toBegin()const;
void toNext()const;
void toPrevious()const;
void toEnd()const;
bool isEmpty()const;
bool isInEnd()const;
bool isAfterEnd()const;
bool isInBegin()const;
void erase();
T& getCurrent();
T getCurrent() const;
private:
struct Lelem{
Lelem* previous_;
Lelem* next_;
T data_;
};
Lelem* tail_;
Lelem* head_;
mutable Lelem* temp_;
void copy(const List2&);
};
template <typename T>
List2<T>::List2(){
head_ =nullptr;
temp_ =nullptr;
tail_ =nullptr;
}
template <typename T>
List2<T>::~List2(){
makeItEmpty();
}
template <typename T>
List2<T>::List2(const List2<T>& list){
copy(list);
}
template <typename T>
bool List2<T>::operator == (const List2& list)const {
if(!isEmpty()){
toBegin();
list.toBegin();
bool sameStatment=isInEnd()==list.isInEnd();
bool dataIsEquals=temp_->data_==list.temp_->data_;
while(sameStatment&&dataIsEquals&&!isInEnd()){
sameStatment=isInEnd()==list.isInEnd();
dataIsEquals=temp_->data_==list.temp_->data_;
toNext();
}
return (sameStatment&&dataIsEquals);
}
else{
return list.isEmpty();
}
}
template <typename T>
bool List2<T>::operator != (const List2& list)const {
if(!isEmpty()){
toBegin();
list.toBegin();
bool sameStatment=isInEnd()==list.isInEnd();
bool dataIsEquals=temp_->data_==list.temp_->data_;
while(sameStatment&&dataIsEquals&&!isInEnd()){
sameStatment=isInEnd()==list.isInEnd();
dataIsEquals=temp_->data_==list.temp_->data_;
toNext();
}
return !(sameStatment&&dataIsEquals);
}
else{
return !list.isEmpty();
}
}
template <typename T>
List2<T>& List2<T>::operator = (const List2<T>& list){
if (this!=&list){
makeItEmpty();
copy(list);
}
return *this;
}
template<typename T>
ostream& operator << (ostream& out,const List2<T>& list){
if(!list.isempty())
{
list.tobegin();
out<<list.getCurrent();
while(!list.inEnd())
{
list.toNext();
out<<' '<<list.getCurrent();
}
}
return out;
}
template <typename T>
void List2<T>::addToBegin(const T type){
Lelem* a=new Lelem ;
a->data_=type;
if(!head_){
temp_=a;
tail_=a;
a->next_=0;
}
else{
a->next_=head_;
}
a->previous_=0;
head_=a;
}
template <typename T>
void List2<T>::add(const T type){
Lelem* a=new Lelem ;
a->data_=type;
if(head_){
if(temp_){
a->previous_=temp_;
a->next_=temp_->next_;
temp_->next_=a;
a->previous_=temp_;
if(!a->next_){
tail_=a;
}
}
else{
throw std::out_of_range("List is no more");
}
}
else{
head_=a;
temp_=a;
tail_=a;
temp_->next_=nullptr;
temp_->previous_=nullptr;
}
}
template <typename T>
void List2<T>::makeItEmpty(){
temp_=head_;
Lelem* a;
while (temp_){
a=temp_->next_;
delete temp_;
temp_=a;
}
head_=nullptr;
tail_=nullptr;
}
template <typename T>
void List2<T>::toBegin() const{
temp_=head_;
}
template <typename T>
void List2<T>::toNext()const {
if(temp_)
temp_=temp_->next_;
else
throw std::out_of_range("Out of range.");
}
template <typename T>
void List2<T>::toPrevious()const{
if(temp_)
temp_=temp_->previous_;
else
throw std::out_of_range("Out of range.");
}
template <typename T>
void List2<T>::toEnd()const{
temp_=tail_;
}
template <typename T>
bool List2<T>::isEmpty()const{
return !head_;
}
template <typename T>
bool List2<T>::isAfterEnd()const{
return !temp_;
}
template <typename T>
bool List2<T>::isInEnd()const{
return temp_==tail_;
}
template <typename T>
bool List2<T>::isInBegin()const{
return temp_==head_;
}
template <typename T>
void List2<T>::erase(){//Óäàëÿåò ýëåìåíò temp_, ïåðåìåùàåò temp_ çà óäàëåííûé èëè temp_==nullptr åñëè temp_ â êîíöå.
if(temp_){
Lelem *a,*b;
b=temp_->previous_;
a=temp_->next_;
if(isInBegin()){
head_=a;
}
if(isInEnd()){
tail_=b;
}
delete temp_;
if(b){
b->next_=a;
}
if(a){
a->previous_=b;
}
else{
temp_=b;
}
}
else{throw std::logic_error("Current element is bad.");
}
}
template <typename T>
T List2<T>::getCurrent()const{
if (!temp_){throw std::logic_error("Current element is bad.");
}
return temp_->data_;
}
template <typename T>
T& List2<T>::getCurrent(){
if (!temp_){throw std::logic_error("Current element is bad.");
}
return temp_->data_;
}
template <typename T>
void List2<T>::copy(const List2<T>& list){
if(!list.isEmpty()){
list.toBegin();
add(list.getCurrent());
toEnd();
while(!list.isInEnd()){
list.toNext();
add(list.getCurrent());
toEnd();
}
temp_=head_;
}
}
#endif