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