From d8f75f1d3c1734e108b91db2eea5ce0a088ff26e Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 6 Dec 2022 18:25:08 +0100 Subject: [PATCH] LinkedList: Add an append_after() method Can be used to append an item after another one instead of at the end of the list. With doubly linked lists, this is extremely easy to achieve (O(1)). --- luna/include/luna/LinkedList.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/luna/include/luna/LinkedList.h b/luna/include/luna/LinkedList.h index e6056a19..e5e1e279 100644 --- a/luna/include/luna/LinkedList.h +++ b/luna/include/luna/LinkedList.h @@ -47,6 +47,20 @@ template class DoublyLinkedList m_count++; } + void append_after(T* base, T* ptr) + { + DoublyLinkedListNode* new_node = (DoublyLinkedListNode*)ptr; + DoublyLinkedListNode* base_node = (DoublyLinkedListNode*)base; + + if (m_end_node == base_node) m_end_node = new_node; + + new_node->m_next_node = base_node->m_next_node; + base_node->m_next_node = new_node; + new_node->m_last_node = base_node; + + m_count++; + } + T* remove(T* ptr) { DoublyLinkedListNode* node = (DoublyLinkedListNode*)ptr;