Skip to content

Commit

Permalink
AK: Add DoublyLinkedList::prepend()
Browse files Browse the repository at this point in the history
Also make it possible to remove() with a value-type Iterator.
  • Loading branch information
awesomekling committed Dec 2, 2019
1 parent 7dc9c90 commit 61f298f
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion AK/DoublyLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ class DoublyLinkedList {
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) {
Expand Down Expand Up @@ -132,7 +142,7 @@ class DoublyLinkedList {
return end();
}

void remove(Iterator& it)
void remove(Iterator it)
{
ASSERT(it.m_node);
auto* node = it.m_node;
Expand Down Expand Up @@ -163,11 +173,27 @@ class DoublyLinkedList {
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; }

Expand Down

0 comments on commit 61f298f

Please sign in to comment.