Skip to content

Commit

Permalink
Merge pull request #15 from nkaaf/implement_method_to_clear_entire_list
Browse files Browse the repository at this point in the history
Clear Lists
  • Loading branch information
nkaaf authored Feb 22, 2022
2 parents 9785e13 + 93f8643 commit 2fe5514
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
20 changes: 18 additions & 2 deletions src/AbstractList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
*/
template <typename T> class AbstractList {
private:
int size = 0; /// Size of the list.
bool mutableList = true; /// Is the list mutable or immutable.
int size = 0; /// Size of the list.
bool mutableList = false; /// Is the list mutable or immutable.

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

/*!
* @brief Reset the size to zero.
*/
void resetSize() { size = 0; }

/*!
* @brief Method to verify if the given index is out of the range of the list
* size.
Expand Down Expand Up @@ -210,13 +215,24 @@ template <typename T> class AbstractList {
return val;
}

/*!
* @brief Remove all elements from the List.
*/
virtual void clear() = 0;

/*!
* @brief Remove the entry at the given index.
*
* @param index Index of element to remove.
*/
virtual void remove(int index) = 0;

/*!
* @brief Remove all elements from the List.
* @note Alias of clear().
*/
void removeAll() { clear(); }

/*!
* @brief Get the number how many elements are saved in the list.
*
Expand Down
38 changes: 22 additions & 16 deletions src/DoubleLinkedList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,7 @@ template <typename T> class DoubleLinkedList : public AbstractList<T> {
/*!
* @brief Destructor of a DoubleLinkedList Object.
*/
~DoubleLinkedList() {
if (head != nullptr) {
Entry *current = head;
Entry *next;
for (int i = 0; i < this->getSize(); ++i) {
next = current->getNext();

if (!this->isMutable()) {
current->freeValue();
}

delete current;
current = next;
}
}
}
~DoubleLinkedList() { this->clear(); }

void addAtIndex(int index, T &value) override {
// it is allowed, that index == this->getSize() to insert it behind the last
Expand Down Expand Up @@ -203,6 +188,27 @@ template <typename T> class DoubleLinkedList : public AbstractList<T> {
this->increaseSize();
};

void clear() override {
if (this->getSize() > 0) {
Entry *current = head;
Entry *next;
for (int i = 0; i < this->getSize(); ++i) {
next = current->getNext();

if (!this->isMutable()) {
current->freeValue();
}

delete current;
current = next;
}
}

this->resetSize();
head = nullptr;
tail = nullptr;
}

void remove(int index) override {
if (this->isIndexOutOfBounds(index)) {
return;
Expand Down
38 changes: 22 additions & 16 deletions src/SingleLinkedList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,7 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
/*!
* @brief Destructor of a SingleLinkedList Object.
*/
~SingleLinkedList() {
if (head != nullptr) {
Entry *current = head;
Entry *next;
for (int i = 0; i < this->getSize(); i++) {
next = current->getNext();

if (!this->isMutable()) {
current->freeValue();
}

delete current;
current = next;
}
}
}
~SingleLinkedList() { this->clear(); }

void addAtIndex(int index, T &value) override {
// it is allowed, that index == this->getSize() to insert it behind the last
Expand Down Expand Up @@ -164,6 +149,27 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
this->increaseSize();
};

void clear() override {
if (this->getSize() > 0) {
Entry *current = head;
Entry *next;
for (int i = 0; i < this->getSize(); ++i) {
next = current->getNext();

if (!this->isMutable()) {
current->freeValue();
}

delete current;
current = next;
}

this->resetSize();
head = nullptr;
tail = nullptr;
}
}

void remove(int index) override {
if (this->isIndexOutOfBounds(index)) {
return;
Expand Down

0 comments on commit 2fe5514

Please sign in to comment.