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

View File

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