implement list destructor
This commit is contained in:
@ -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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user