AK: Add insert_{before,after}(iterator, value) to SinglyLinkedList

This commit is contained in:
Conrad Pankoff 2019-12-27 11:56:52 +11:00 committed by Andreas Kling
parent abdd5aa08a
commit 13cf7e76b9
Notes: sideshowbarker 2024-07-19 10:38:19 +09:00

View file

@ -194,6 +194,58 @@ public:
delete iterator.m_node;
}
void insert_before(Iterator iterator, const T& value)
{
auto* node = new Node(value);
node->next = iterator.m_node;
if (m_head == iterator.m_node)
m_head = node;
if (iterator.m_prev)
iterator.m_prev->next = node;
}
void insert_before(Iterator iterator, T&& value)
{
auto* node = new Node(move(value));
node->next = iterator.m_node;
if (m_head == iterator.m_node)
m_head = node;
if (iterator.m_prev)
iterator.m_prev->next = node;
}
void insert_after(Iterator iterator, const T& value)
{
if (iterator.is_end()) {
append(value);
return;
}
auto* node = new Node(value);
node->next = iterator.m_node->next;
iterator.m_node->next = node;
if (m_tail == iterator.m_node)
m_tail = node;
}
void insert_after(Iterator iterator, T&& value)
{
if (iterator.is_end()) {
append(value);
return;
}
auto* node = new Node(move(value));
node->next = iterator.m_node->next;
iterator.m_node->next = node;
if (m_tail == iterator.m_node)
m_tail = node;
}
private:
Node* head() { return m_head; }
const Node* head() const { return m_head; }