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:
parent
146da13e43
commit
d8f75f1d3c
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user