1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +00:00

Make SinglyLinkedList destruction actually work.

This commit is contained in:
Andreas Kling 2018-10-13 14:29:00 +02:00
parent 6ea8ce500c
commit b7efd92937

View file

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