This came about from an issue I had with the usual Remove method for linked lists, because it would only remove the first instance of any particular value. I was using lists that had to be ordered in a particular fashion, and I didn't always want to remove the first instance of a value, but a value at a particular index. You can find the value at a particular index with ValueAtIndex, so obviously it wasn't absent because of a conflict in functionality. As a result, I patched in a method called RemoveAtIndex, which serves basically as a combination of Remove and ValueAtIndex, which also returns the value of the list entry you just removed, like the Remove method does. The following code goes into linkedlist.bmx immediately after the Remove method in TList.
Rem
bbdoc: Removes the link at the given index.
about: Throws an exception if the index is out of range (must be 0..list.Count()-1 inclusive).
End Rem
Method RemoveAtIndex:Object( index )
Assert index>=0 Else "Object index must be positive"
Local link:TLink=_head._succ
While link<>_head
If Not index Then
link.Remove()
Return link._value
EndIf
link=link._succ
index:-1
Wend
RuntimeError "List index out of range"
End Method
Hope this helps someone.
Last edited 2012
Last edited 2012
|