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