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)).
This commit is contained in:
apio 2022-12-06 18:25:08 +01:00
parent 146da13e43
commit d8f75f1d3c
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -47,6 +47,20 @@ template <typename T> class DoublyLinkedList
m_count++;
}
void append_after(T* base, T* ptr)
{
DoublyLinkedListNode<T>* new_node = (DoublyLinkedListNode<T>*)ptr;
DoublyLinkedListNode<T>* base_node = (DoublyLinkedListNode<T>*)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<T>* node = (DoublyLinkedListNode<T>*)ptr;