-
Notifications
You must be signed in to change notification settings - Fork 0
/
aghSlist.h
250 lines (220 loc) · 6.01 KB
/
aghSlist.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
#ifndef AGHSLIST_H
#define AGHSLIST_H
template<class T>
class aghSlist: public aghContainer<T>{
int length;
template<class D>
class element{
public:
D data;
element<D>* next;
element(): next(NULL) {}
element(element<D>* _next, D _data) :
next(_next), data(_data) {}
};
element<T> *first;
public:
aghSlist(): first(NULL), length(0) {}
aghSlist(const aghSlist& other);
~aghSlist();
aghSlist(const aghContainer<T>& other);
/// \brief Metoda dodajaca wartosc do pojemnika
///
/// \param index - indeks wstawianego elementu
/// \param value - wstawiana wartosc
/// \return Wartosc logiczna informujaca o powodzeniu operacji
virtual bool insert(int index, T const& value);
/// \brief Metoda pobierajaca wartosc z pojemnika
///
/// \param index - indeks pobieranego elementu
/// \return Pobierana wartosc
virtual T& at(int index) const;
/// \brief Metoda pobierajaca ilosc elementow pojemnika
///
/// \return Ilosc elementow w pojemniku
virtual int size(void) const;
/// \brief Metoda usuwajaca wartosc z pojemnika
///
/// \param index - indeks usuwanego elementu
/// \return Wartosc logiczna informujaca o powodzeniu operacji
virtual bool remove(int index);
aghSlist<T>& operator=(aghSlist<T> const& other);
aghSlist<T>& operator=(aghContainer<T> const& other);
};
template<class T>
aghSlist<T>::aghSlist(const aghSlist& other): first(NULL), length(0){
element<T> *temp;
length = other.size();
first = new element<T>; // WYJATKI!
first->data = other.at(0);
temp = first;
for(int i=1;i<length;i++){
temp->next = new element<T>;
temp = temp->next;
temp->data = other.at(i);
}
}
template<class T>
aghSlist<T>::aghSlist(const aghContainer<T>& other): first(NULL), length(0){
element<T> *temp;
length = other.size();
first = new element<T>; // WYJATKI!
first->data = other.at(0);
temp = first;
for(int i=1;i<length;i++){
temp->next = new element<T>;
temp = temp->next;
temp->data = other.at(i);
}
}
template<class T>
aghSlist<T>& aghSlist<T>::operator=(aghSlist<T> const& other){
if(this == &other) return *this;
element<T> *temp;
if(length == other.size()){
temp = first;
for(int i=0;i<length;i++){
temp->data = other.at(i);
}
}
else{
while(length > 1){
temp = first;
for(int i=0;i<length-1;i++) temp = temp->next;
delete temp;
length--;
}
delete first;
length = other.size();
first = new element<T>; // WYJATKI!
first->data = other.at(0);
temp = first;
for(int i=1;i<length;i++){
temp->next = new element<T>;
temp = temp->next;
temp->data = other.at(i);
}
}
return *this;
}
template<class T>
aghSlist<T>& aghSlist<T>::operator=(aghContainer<T> const& other){
if(this == &other) return *this;
element<T> *temp;
if(length == other.size()){
temp = first;
for(int i=0;i<length;i++){
temp->data = other.at(i);
}
}
else{
while(length > 1){
temp = first;
for(int i=0;i<length-1;i++) temp = temp->next;
delete temp;
length--;
}
delete first;
length = other.size();
first = new element<T>; // WYJATKI!
first->data = other.at(0);
temp = first;
for(int i=1;i<length;i++){
temp->next = new element<T>;
temp = temp->next;
temp->data = other.at(i);
}
}
return *this;
}
template<class T>
aghSlist<T>::~aghSlist(){
element<T>* temp = first;
while(length > 1){
temp = first;
for(int i=0;i<length-1;i++) temp = temp->next;
delete temp;
length--;
}
delete first;
length = 0;
}
template <class T>
bool aghSlist<T>::insert(int index, T const& value){
if(index < 0 || index > length){
return false;
}
element<T> *temp;
element<T> *new_el = new element<T>; // WYJATEK!
new_el->data = value;
if(index == 0){
if(first == NULL){
first = new_el;
new_el->next = NULL;
}
else{
temp = first;
first = new_el;
new_el->next = temp;
}
}
else{
temp = first;
for(int i=0;i<index-1;i++){
temp = temp->next;
}
if(temp->next == NULL){
temp->next = new_el;
new_el->next = NULL;
}
else{
// TUUUUUUUUU!
}
}
length++;
return true;
}
template<class T>
T& aghSlist<T>::at(int index) const{
if(index < 0 || index > length){
throw aghException(4,"Blad zakresu", __FILE__, __LINE__);
}
else{
element<T> *temp = first;
for(int i=0;i<index;i++){
temp = temp->next;
}
return temp->data;
}
}
template<class T>
int aghSlist<T>::size(void) const{
return length;
}
template<class T>
bool aghSlist<T>::remove(int index){
if(index < 0 || index > length){
return false;
}
else{
element<T>* temp = first;
if(index == 0){
first = temp->next;
delete temp;
length--;
}
else{
for(int i=1;i<index;i++){
temp = temp->next;
}
element<T>* to_rem = temp->next;
if(to_rem->next != NULL) temp->next = to_rem->next;
else temp->next = NULL;
delete to_rem;
length--;
}
if(length == 0) first = NULL;
return true;
}
}
#endif