easy way to change object position in list?

BlitzMax Forums/BlitzMax Programming/easy way to change object position in list?

Hi all,

I want to change the order of objects in my linked list. Is there a really easy way of doing this?

You could manually modify the _succ and _pred Fields of the TLink

How would I do this? sorry I'm not very experienced in bmax yet.

I was thinking on it and you don't really need to mess around with the _pred and _succ fields at all. Just the value

Type TItem
	Field val:Int
End Type

Global myList:Tlist = New TList
Global i:Int
Global tempItem:TItem

For i = 0 To 10
	tempItem = New TItem
		tempItem.val = i
	myList.AddLast(tempItem)
Next

Print "First sequence"
Print
'Print items in sequence
For tempItem = EachIn myList
	Print tempItem.val
Next
Print
Print "Swapping"
Print
swapnums(3,7)

Print "Second Sequence"
Print
'Print items in sequence
For tempItem = EachIn myList
	Print tempItem.val
Next


Function swapnums(num1, num2)
	Local currentLink:TLink
	Local Link1:TLink
	Local Link2:TLink
	
	currentLink = myList.FirstLink()
	
	While (Link1 = Null Or  Link2 = Null)
		If TItem(currentLink._value).val = num1 Then Link1 = currentLink
		If TItem(currentLink._value).val = num2 Then Link2 = currentLink
		
		
		If currentLink = myList.LastLink() Then Exit
		
		currentLink = currentLink.NextLink()
		
	Wend

	SwapLinks(Link1, Link2)
	
End Function


Function SwapLinks(Link1:TLink, Link2:TLink)
	Local tempVal:Object
	tempVal = Link1._value
	
	Link1._value = Link2._value
	Link2._value = tempVal
End Function


how about...

Local list:TList = CreateList()
Local i:Int, t:test

Type test
	Field x
End Type

For i = 0 Until 10
	t:test = New test
	t.x = i
	ListAddLast list,t
Next

For t = EachIn list
	Print t.x
Next
Print

swaplinks list,0,9

For t = EachIn list
	Print t.x
Next

Function swaplinks(list:TList Var, s:Int, d:Int)
	Local a:Object, b:Object, c:Object, ar:Object[]
	
	ar = ListToArray(list)
	c = ar[s]
	ar[s] = ar[d]
	ar[d] = c
	
	list = ListFromArray(ar)
End Function


Thanks lads that looks great :)

SwapLinks looks good. How do I find the current link? I saw a command called FindLink but I am not sure how to use it. Ideally I'd use this function:

Function SwapLinks(Link1:TLink, Link2:TLink)
	Local tempVal:Object
	tempVal = Link1._value
	
	Link1._value = Link2._value
	Link2._value = tempVal
End Function


And just pass the two links in and it should work? Or am I way off base?

And just pass the two links in and it should work? Or am I way off base?


That's pretty much how you would use it.

The problem with using findlink is that you need to have a variable that points to the object you want to find in the first place.

The easiest way to do it is as I have in SwapNums, that is, loop through the list and compare each TLinks until you find the ones you need.