[BLR.LinkedList] Delete value at index

BlitzMax Forums/BlitzMax Module Tweaks/[BLR.LinkedList] Delete value at index

zcbeaton(Posted 2012) [#1]
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


d-bug(Posted 2012) [#2]
. sorry ... Haven't read the whole text...

Last edited 2012


Streaksy(Posted 2016) [#3]
Yay


Kryzon(Posted 2016) [#4]
Hi. Are you sure Assert is used like that? In the documentation it seems it's a function with the test and the error message as parameters.
http://www.blitzbasic.com/bmdocs/command.php?name=assert

Assert( index >= 0, "Object index must be positive" )