Skip to content

Commit 2fe5514

Browse files
authored
Merge pull request #15 from nkaaf/implement_method_to_clear_entire_list
Clear Lists
2 parents 9785e13 + 93f8643 commit 2fe5514

File tree

3 files changed

+62
-34
lines changed

3 files changed

+62
-34
lines changed

src/AbstractList.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
*/
3737
template <typename T> class AbstractList {
3838
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.
4141

4242
protected:
4343
/// Sometimes it is allowed, that index == this->getSize() to insert it behind
@@ -97,6 +97,11 @@ template <typename T> class AbstractList {
9797
*/
9898
void decreaseSize() { size--; }
9999

100+
/*!
101+
* @brief Reset the size to zero.
102+
*/
103+
void resetSize() { size = 0; }
104+
100105
/*!
101106
* @brief Method to verify if the given index is out of the range of the list
102107
* size.
@@ -210,13 +215,24 @@ template <typename T> class AbstractList {
210215
return val;
211216
}
212217

218+
/*!
219+
* @brief Remove all elements from the List.
220+
*/
221+
virtual void clear() = 0;
222+
213223
/*!
214224
* @brief Remove the entry at the given index.
215225
*
216226
* @param index Index of element to remove.
217227
*/
218228
virtual void remove(int index) = 0;
219229

230+
/*!
231+
* @brief Remove all elements from the List.
232+
* @note Alias of clear().
233+
*/
234+
void removeAll() { clear(); }
235+
220236
/*!
221237
* @brief Get the number how many elements are saved in the list.
222238
*

src/DoubleLinkedList.hpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,7 @@ template <typename T> class DoubleLinkedList : public AbstractList<T> {
126126
/*!
127127
* @brief Destructor of a DoubleLinkedList Object.
128128
*/
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(); }
145130

146131
void addAtIndex(int index, T &value) override {
147132
// 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> {
203188
this->increaseSize();
204189
};
205190

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+
206212
void remove(int index) override {
207213
if (this->isIndexOutOfBounds(index)) {
208214
return;

src/SingleLinkedList.hpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,7 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
104104
/*!
105105
* @brief Destructor of a SingleLinkedList Object.
106106
*/
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(); }
123108

124109
void addAtIndex(int index, T &value) override {
125110
// 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> {
164149
this->increaseSize();
165150
};
166151

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+
167173
void remove(int index) override {
168174
if (this->isIndexOutOfBounds(index)) {
169175
return;

0 commit comments

Comments
 (0)