Skip to content

Commit

Permalink
add new addAll method, rewrite nested 'if'
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaaf committed Oct 26, 2023
1 parent 182bead commit 589ce83
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
20 changes: 18 additions & 2 deletions src/AbstractList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class AbstractList {
#endif

/*!
* @brief Add all entries from the given list to this list at the given index.
* @brief Add all entries from the given list to this list at a specified index.
* The original entry at this index, and followings, will be placed
* directly after the entries of the given list.
* @note The elements from the other list, remain untouched.
Expand All @@ -175,7 +175,23 @@ class AbstractList {
void addAll(AbstractList<T> &list) { addAll(getSize(), list); }

/*!
* @brief Add all entries from the given array.
* @brief Add all entries from an array to this list at a specified index.
* The original entry at this index, and followings, will be placed
* directly after the entries of the given list.
* @note The elements from the other list, remain untouched.
*
* @param index Index of tehis list, at which all entries should be added.
* @param arr Array.
* @param arrSize Size of the array.
*/
void addAll(int index, T* arr, size_t arrSize) {
for (size_t i = 0; i < arrSize; ++i) {
addAtIndex(index++, arr[i]);
}
}

/*!
* @brief Add all entries from an array.
* @note The elements from the other list, remain untouched.
*
* @param arr Array.
Expand Down
27 changes: 15 additions & 12 deletions src/SingleLinkedList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class SingleLinkedList : public AbstractList<T> {
Entry *entry;

if (this->isMutable()) {
//entry = new Entry(*value);
// TODO:
entry = new Entry(*value);
} else {
entry = new Entry();
entry->setValue(value);
Expand Down Expand Up @@ -156,20 +157,22 @@ class SingleLinkedList : public AbstractList<T> {
* @copydoc AbstractList::clear()
*/
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->getSize() == 0) {
return;
}

delete current;
current = next;
}
Entry *current = head;
Entry *next;
for (int i = 0; i < this->getSize(); ++i) {
next = current->getNext();

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

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

/*!
Expand Down

0 comments on commit 589ce83

Please sign in to comment.