Question about TList.Reverse Command.
BlitzMax Forums/BlitzMax Programming/Question about TList.Reverse Command.
| ||
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? |
| ||
I have a feeling it's the latter... that would seem the smart option. |
| ||
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 |
| ||
Thanks for that! I feel kinda of stupid because I could of wrote that example myself. Anyway I think I better go to bed. |
| ||
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? |
| ||
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. |
| ||
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. |
| ||
I am sure there is one in the code archives somewhere |
| ||
I am sure there is one in the code archives somewhere I'll check it out. |
| ||
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 |
| ||
doubly linked list...hmmm That shares some light on the inner workings of BlitzMax. |
| ||
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. |