Linked List

BlitzMax Forums/BlitzMax Module Tweaks/Linked List

Bot Builder(Posted 2005) [#1]
A few handy methods/functions for outside of type:

	Method GetLink:TLink(Index) Private
		Assert index>=0 Else "Object index must be positive"
		Local i=0,link:TLink=_head._succ
		While i<>index
			link=link._succ
			?Debug
			If link=null Then RuntimeError "RemoveIndex Index out of range"
			?
			i:+1
		Wend
		Return link
	End Method

	Method RemoveIndex(Index)
		GetLink(Index).Remove()
	End Method

	Method AddBefore:TLink( Index, value:Object )
		Return InsertBeforeLink( value, GetLink(Index) )
	End Method

	Method AddAfter:TLink( Index, value:Object )
		Return InsertAfterLink( value, GetLink(Index) )
	End Method

Rem
bbdoc: Remove an object from a linked list
about: Uses an index rather than a value
End Rem
Function ListRemoveIndex( List:TList , Index )
	List.RemoveIndex( Index )
End Function

Rem
bbdoc: Adds an object before the specified index
returns: A link object
End Rem
Function ListAddBefore:TLink( List:TList , Index, o:object )
	Return List.AddBefore( Index, o )
End Function

Rem
bbdoc: Adds an object after the specified index
returns: A link object
End Rem
Function ListAddAfter:TLink( List:TList , Index, o:object )
	Return List.AddAfter( Index, o )
End Function



Bot Builder(Posted 2005) [#2]
Oh, a slight mod to the type that allows you to access the current enum of a For..eachin..next loop:

	Field Enum:TListEnum	'Additional field

	Method ObjectEnumerator:TListEnum()	'Modified Method
		enum=New TListEnum
		enum._link=_head._succ
		Return enum
	End Method


Then, you can access the enumerator, to get it to skip elements, or return the current link.