Make SinglyLinkedList destruction actually work.

This commit is contained in:
Andreas Kling 2018-10-13 14:29:00 +02:00
parent 6ea8ce500c
commit b7efd92937
Notes: sideshowbarker 2024-07-19 18:49:09 +09:00

View File

@ -8,16 +8,28 @@ template<typename T>
class SinglyLinkedList {
private:
struct Node {
explicit Node(T&& v) : value(v) { }
T value;
Node* next { nullptr };
};
public:
SinglyLinkedList() { }
~SinglyLinkedList() { }
~SinglyLinkedList() { clear(); }
bool isEmpty() const { return !head(); }
void clear()
{
for (auto* node = m_head; node; ) {
auto* next = node->next;
delete node;
node = next;
}
m_head = nullptr;
m_tail = nullptr;
}
T& first() { ASSERT(head()); return head()->value; }
const T& first() const { ASSERT(head()); return head()->value; }
T& last() { ASSERT(head()); return tail()->value; }
@ -25,8 +37,7 @@ public:
void append(T&& value)
{
auto* node = new Node;
node->value = std::move(value);
auto* node = new Node(std::move(value));
if (!m_head) {
m_head = node;
m_tail = node;