File tree Expand file tree Collapse file tree 3 files changed +62
-34
lines changed Expand file tree Collapse file tree 3 files changed +62
-34
lines changed Original file line number Diff line number Diff line change 36
36
*/
37
37
template <typename T> class AbstractList {
38
38
private:
39
- int size = 0 ; // / Size of the list.
40
- bool mutableList = true ; // / Is the list mutable or immutable.
39
+ int size = 0 ; // / Size of the list.
40
+ bool mutableList = false ; // / Is the list mutable or immutable.
41
41
42
42
protected:
43
43
// / Sometimes it is allowed, that index == this->getSize() to insert it behind
@@ -97,6 +97,11 @@ template <typename T> class AbstractList {
97
97
*/
98
98
void decreaseSize () { size--; }
99
99
100
+ /* !
101
+ * @brief Reset the size to zero.
102
+ */
103
+ void resetSize () { size = 0 ; }
104
+
100
105
/* !
101
106
* @brief Method to verify if the given index is out of the range of the list
102
107
* size.
@@ -210,13 +215,24 @@ template <typename T> class AbstractList {
210
215
return val;
211
216
}
212
217
218
+ /* !
219
+ * @brief Remove all elements from the List.
220
+ */
221
+ virtual void clear () = 0;
222
+
213
223
/* !
214
224
* @brief Remove the entry at the given index.
215
225
*
216
226
* @param index Index of element to remove.
217
227
*/
218
228
virtual void remove (int index) = 0;
219
229
230
+ /* !
231
+ * @brief Remove all elements from the List.
232
+ * @note Alias of clear().
233
+ */
234
+ void removeAll () { clear (); }
235
+
220
236
/* !
221
237
* @brief Get the number how many elements are saved in the list.
222
238
*
Original file line number Diff line number Diff line change @@ -126,22 +126,7 @@ template <typename T> class DoubleLinkedList : public AbstractList<T> {
126
126
/* !
127
127
* @brief Destructor of a DoubleLinkedList Object.
128
128
*/
129
- ~DoubleLinkedList () {
130
- if (head != nullptr ) {
131
- Entry *current = head;
132
- Entry *next;
133
- for (int i = 0 ; i < this ->getSize (); ++i) {
134
- next = current->getNext ();
135
-
136
- if (!this ->isMutable ()) {
137
- current->freeValue ();
138
- }
139
-
140
- delete current;
141
- current = next;
142
- }
143
- }
144
- }
129
+ ~DoubleLinkedList () { this ->clear (); }
145
130
146
131
void addAtIndex (int index, T &value) override {
147
132
// it is allowed, that index == this->getSize() to insert it behind the last
@@ -203,6 +188,27 @@ template <typename T> class DoubleLinkedList : public AbstractList<T> {
203
188
this ->increaseSize ();
204
189
};
205
190
191
+ void clear () override {
192
+ if (this ->getSize () > 0 ) {
193
+ Entry *current = head;
194
+ Entry *next;
195
+ for (int i = 0 ; i < this ->getSize (); ++i) {
196
+ next = current->getNext ();
197
+
198
+ if (!this ->isMutable ()) {
199
+ current->freeValue ();
200
+ }
201
+
202
+ delete current;
203
+ current = next;
204
+ }
205
+ }
206
+
207
+ this ->resetSize ();
208
+ head = nullptr ;
209
+ tail = nullptr ;
210
+ }
211
+
206
212
void remove (int index) override {
207
213
if (this ->isIndexOutOfBounds (index)) {
208
214
return ;
Original file line number Diff line number Diff line change @@ -104,22 +104,7 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
104
104
/* !
105
105
* @brief Destructor of a SingleLinkedList Object.
106
106
*/
107
- ~SingleLinkedList () {
108
- if (head != nullptr ) {
109
- Entry *current = head;
110
- Entry *next;
111
- for (int i = 0 ; i < this ->getSize (); i++) {
112
- next = current->getNext ();
113
-
114
- if (!this ->isMutable ()) {
115
- current->freeValue ();
116
- }
117
-
118
- delete current;
119
- current = next;
120
- }
121
- }
122
- }
107
+ ~SingleLinkedList () { this ->clear (); }
123
108
124
109
void addAtIndex (int index, T &value) override {
125
110
// it is allowed, that index == this->getSize() to insert it behind the last
@@ -164,6 +149,27 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
164
149
this ->increaseSize ();
165
150
};
166
151
152
+ void clear () override {
153
+ if (this ->getSize () > 0 ) {
154
+ Entry *current = head;
155
+ Entry *next;
156
+ for (int i = 0 ; i < this ->getSize (); ++i) {
157
+ next = current->getNext ();
158
+
159
+ if (!this ->isMutable ()) {
160
+ current->freeValue ();
161
+ }
162
+
163
+ delete current;
164
+ current = next;
165
+ }
166
+
167
+ this ->resetSize ();
168
+ head = nullptr ;
169
+ tail = nullptr ;
170
+ }
171
+ }
172
+
167
173
void remove (int index) override {
168
174
if (this ->isIndexOutOfBounds (index)) {
169
175
return ;
You can’t perform that action at this time.
0 commit comments