Question about TList.Reverse Command.

BlitzMax Forums/BlitzMax Programming/Question about TList.Reverse Command.

JoJo(Posted 2008) [#1]
I'm probably making this harder than its need to be.

But when you use the Reverse command does that mean that last object in the list now becomes the first piece and the second to last piece now becomes the second piece and so on,

or

does it simply means that the objects in the list is being read from the last piece to the first piece?


Czar Flavius(Posted 2008) [#2]
I have a feeling it's the latter... that would seem the smart option.


tonyg(Posted 2008) [#3]
The first one.
It reverses the order of the list
SuperStrict
Local mylist:TList=CreateList()
mylist.addlast("one")
mylist.addlast("two")
mylist.addlast("three")
mylist.addlast("four")
mylist.addlast("five")
Print "*** FORWARD ***"
For Local all:String = EachIn mylist
	Print all
Next
mylist.reverse()
Print "*** AND BACK ***"

For Local all:String = EachIn mylist
	Print all
Next



JoJo(Posted 2008) [#4]
Thanks for that!

I feel kinda of stupid because I could of wrote that example myself. Anyway I think I better go to bed.


Czar Flavius(Posted 2008) [#5]
I don't understand how you can tell the difference. Wouldn't it be more efficient simply to traverse the links in reverse order, instead of having to move things around?


tonyg(Posted 2008) [#6]
Not sure how to answer that Czar.
The source shows that it switches the first and last links and then switches the prevlink/nextlink pointers for each list entry.
I agree a 'list_backwards' command is useful and quite easy to write. However, it would be more useful as a one-off.
However, if you had a flag set for reversing the list it would be quite confusing what addlast and addfirst would do.


JoJo(Posted 2008) [#7]
A list_backwards command would work beautifully for me. I need to just read through the list from the last object in the list to the first one.

I'm going to give it a go and try to write that functionality.


tonyg(Posted 2008) [#8]
I am sure there is one in the code archives somewhere


JoJo(Posted 2008) [#9]

I am sure there is one in the code archives somewhere



I'll check it out.


Blueapples(Posted 2008) [#10]
TList is a doubly linked list: each link contains a reference to the previous link as well as the next link (and of course the value for that cell). The first link's PrevLink is null, the last link's NextLink is null. All you have to do is walk through from the list's LastLink to it's FirstLink.

SuperStrict
Local mylist:TList=CreateList()
mylist.addlast("one")
mylist.addlast("two")
mylist.addlast("three")
mylist.addlast("four")
mylist.addlast("five")
Print "*** BACKWARD - NO EXTRA WORK ***"
If Not mylist.IsEmpty()					' The loop will cause an error if you run it on an empty list
	Local link:TLink = mylist.LastLink()	' Get the last link
	Repeat
		Print String(link.Value())		' Value() returns the object corresponding to this link
		link = link.PrevLink()			' If PrevLink() returns null, then we are at the first link
	Until link = Null					' So stop looping
EndIf



JoJo(Posted 2008) [#11]
doubly linked list...hmmm

That shares some light on the inner workings of BlitzMax.


tonyg(Posted 2008) [#12]
One problem might be knowing which type to cast the link.value() object back to if the list contains diffrent types.
I don't know a nice way around this so if anybody else does...
<edit> Wouldn't really want to add a field naming the type within each instance.