diff --git a/AK/SinglyLinkedList.h b/AK/SinglyLinkedList.h index 03021b0ee8d..68f87e32762 100644 --- a/AK/SinglyLinkedList.h +++ b/AK/SinglyLinkedList.h @@ -8,16 +8,28 @@ template 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;