mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Add insert_{before,after}(iterator, value) to SinglyLinkedList
This commit is contained in:
parent
abdd5aa08a
commit
13cf7e76b9
Notes:
sideshowbarker
2024-07-19 10:38:19 +09:00
Author: https://github.com/deoxxa Commit: https://github.com/SerenityOS/serenity/commit/13cf7e76b92 Pull-request: https://github.com/SerenityOS/serenity/pull/925
1 changed files with 52 additions and 0 deletions
|
@ -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; }
|
||||
|
|
Loading…
Reference in a new issue