1

implement list destructor

This commit is contained in:
2022-07-19 14:47:49 +02:00
parent 304458a816
commit 18c43ae37d
2 changed files with 24 additions and 11 deletions

View File

@ -65,6 +65,17 @@ private:
}
public:
~LinkedList() override {
typename Iterator::Type* current = head;
typename Iterator::Type* next;
while (current != NULL) {
next = current->next;
delete current;
current = next;
}
}
Iterator begin() override {
return Iterator(this->head);
}
@ -255,18 +266,18 @@ public:
}
void print(OutStream& out) const override {
if (this->empty()) {
out << "Print List (0 elements)" << endl;
return;
}
// if (this->empty()) {
// out << "Print List (0 elements)" << endl;
// return;
// }
out << "Print List (" << dec << this->size() << " elements): ";
typename Iterator::Type* current = this->head;
while (current != NULL) {
out << dec << *current << " ";
current = current->next;
}
out << endl;
// out << "Print List (" << dec << this->size() << " elements): ";
// typename Iterator::Type* current = this->head;
// while (current != NULL) {
// out << dec << *current << " ";
// current = current->next;
// }
// out << endl;
}
};

View File

@ -9,6 +9,8 @@
template<typename T, typename I = Iterator<T>>
class List {
public:
virtual ~List() = 0;
using Type = T; // We make the template argument accessible from the subclasses
using Iterator = I; // Needed for range based for loop