Added a Remove() version to which the previous element needs to be passed as

well. This is rather ugly, but it was the quickest way to provide O(1) element
removal. This class could really use some love.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39296 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2010-11-04 14:58:10 +00:00
parent b502275bc8
commit 5609b854b5

View File

@ -1,6 +1,6 @@
/*
* Copyright 2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Copyright 2005-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2005-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
*
* Distributed under the terms of the MIT License.
*/
@ -143,6 +143,7 @@ class SinglyLinkedList {
inline void Add(Element* element);
inline void Remove(Element* element);
inline void Remove(Element* previous, Element* element);
inline void MoveFrom(SINGLY_LINKED_LIST_CLASS_NAME* fromList);
// O(1) if either list is empty, otherwise O(n).
@ -182,7 +183,7 @@ SINGLY_LINKED_LIST_CLASS_NAME::Add(Element* element)
}
}
// Remove
SINGLY_LINKED_LIST_TEMPLATE_LIST
void
SINGLY_LINKED_LIST_CLASS_NAME::Remove(Element* element)
@ -207,6 +208,23 @@ SINGLY_LINKED_LIST_CLASS_NAME::Remove(Element* element)
}
SINGLY_LINKED_LIST_TEMPLATE_LIST
void
SINGLY_LINKED_LIST_CLASS_NAME::Remove(Element* previous, Element* element)
{
// ASSERT(previous == NULL
// ? fFirst == element : sGetLink(previous)->next == element);
Link* elementLink = sGetLink(element);
if (previous == NULL)
fFirst = elementLink->next;
else
sGetLink(previous)->next = elementLink->next;
elementLink->next = NULL;
}
SINGLY_LINKED_LIST_TEMPLATE_LIST
void
SINGLY_LINKED_LIST_CLASS_NAME::MoveFrom(SINGLY_LINKED_LIST_CLASS_NAME* fromList)