AK: Add DoublyLinkedList::prepend()

Also make it possible to remove() with a value-type Iterator.
This commit is contained in:
Andreas Kling 2019-12-02 18:36:10 +01:00
parent 7dc9c90f83
commit 61f298faf3

View file

@ -95,6 +95,16 @@ public:
append_node(new Node(value));
}
void prepend(T&& value)
{
prepend_node(new Node(move(value)));
}
void prepend(const T& value)
{
prepend_node(new Node(value));
}
bool contains_slow(const T& value) const
{
for (auto* node = m_head; node; node = node->next) {
@ -132,7 +142,7 @@ public:
return end();
}
void remove(Iterator& it)
void remove(Iterator it)
{
ASSERT(it.m_node);
auto* node = it.m_node;
@ -163,11 +173,27 @@ private:
return;
}
ASSERT(m_tail);
ASSERT(!node->next);
m_tail->next = node;
node->prev = m_tail;
m_tail = node;
}
void prepend_node(Node* node)
{
if (!m_head) {
ASSERT(!m_tail);
m_head = node;
m_tail = node;
return;
}
ASSERT(m_tail);
ASSERT(!node->prev);
m_head->prev = node;
node->next = m_head;
m_head = node;
}
Node* head() { return m_head; }
const Node* head() const { return m_head; }