From 13cf7e76b929fd99b05be0b3d0170d398537fa3c Mon Sep 17 00:00:00 2001 From: Conrad Pankoff Date: Fri, 27 Dec 2019 11:56:52 +1100 Subject: [PATCH] AK: Add insert_{before,after}(iterator, value) to SinglyLinkedList --- AK/SinglyLinkedList.h | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/AK/SinglyLinkedList.h b/AK/SinglyLinkedList.h index 9ede7a82466..3c6adff643c 100644 --- a/AK/SinglyLinkedList.h +++ b/AK/SinglyLinkedList.h @@ -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; }