Hi all,
I want to change the order of objects in my linked list. Is there a really easy way of doing this?
I want to change the order of objects in my linked list. Is there a really easy way of doing this?
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
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
Function SwapLinks(Link1:TLink, Link2:TLink) Local tempVal:Object tempVal = Link1._value Link1._value = Link2._value Link2._value = tempVal End Function