don't understand Tlink
BlitzMax Forums/BlitzMax Beginners Area/don't understand Tlink
| ||
Could someone could explain with an example what Tlink is and what used for ?Mylist:TList=createlist() listaddlast Mylist,"one" listaddlast Mylist,"two" listaddlast Mylist,"three" for a$=eachin Mylist print a$ next perhaps a method to navigate into this list. So you can automaticaly store an index and obtain the next item ? for example : next item into MyList is "two" ? next will be "three" ! |
| ||
Perhaps my question is not clear ! (pseudo code) MyList = ("one","two","three") is there a method to have MyVar = next (MyList) ' returns 1 MyVar = next (MyList) ' returns 2 MyVar = next (MyList) ' returns 3 MyVar = next (MyList) ' returns 1 |
| ||
TLink is an object with a reference to an element inside a list. You can use its methods for various purposes. I usually store an element's TLink to remove itself from the list without having to know which list he is in, with Link.Remove (this is an example) If you want to navigate the list use a FOR EACH : Local Obj:Object Local MyElement:MyType For Obj = EachIn MyList MyElement = MyType (Obj) 'casting object to my type [...] 'code here Next |
| ||
Look for prevlink and nextlink in the doc. There have been lots of examples and comments made on these forums. |
| ||
Tlink is a pointer to an element in a Linked ListLocal mylist:Tlist = CreateList() Local link1:TLink = ListAddLast(mylist,"pie") Local link2:TLink = ListAddLast(mylist,"Ice Cream") Local link3:TLink = ListAddLast(mylist,"Liver") Local link4:TLink = ListAddLast(mylist,"Chocolate Syrup") For s$ = EachIn mylist Print s$ Next Print "----------------------------------------------" Print "Let's Get Rid of Liver!!!" RemoveLink(link3) For s$ = EachIn mylist Print s$ Next |
| ||
Thanks for your help. Also thanks to Jim Brown for this : For handling lists of strings I use this method: First, I create a TList: Global mylist:TList=New TList To add strings to the list: mylist.AddLast "Apple" mylist.AddLast "Banana" mylist.AddLast "Pear" etc.. Now I create a TLink and point it the the FIRST item in my list: Global link:TLink=mylist.FirstLink() From here, I can iterate through the list both forwards and backwards. (Note how I wrap around the list if I go past the start/end) ... FORWARDS: link=link.NextLink() If link=Null link=mylist.FirstLink() BACKWARDS: link=link.PrevLink() If link=Null link=mylist.LastLink() If I want to find out the contents of the item in the list which 'link' is currently pointing at I do this: text$=String(link.Value()) To insert an entry AFTER the one being pointed to by the TLink: mylist.InsertAfterLink("Some new fruit",link) To insert an entry BEFORE the one being pointed to by the TLink: mylist.InsertBeforeLink("Some new fruit",link) Putting it all together: Strict ' Initialise a TList called mylist ' This is used to hold a list of strings Global mylist:TList=New TList ' add some items to mylist mylist.AddLast "Apple" mylist.AddLast "Banana" mylist.AddLast "Pear" mylist.AddLast "Blackberry" mylist.AddLast "Peach" mylist.AddLast "Mango" ' Initialise a TLink called link ' Set it to point to the first item in mylist Global link:TLink=mylist.FirstLink() ' store the string from the first item into selected$ Global selected$=String(link.Value()) ' open a small graphic window AppTitle = "Lists - Next/Previous example" Graphics 400,300,0,30|SOFTSYNC SetClsColor $aa,$aa,$aa Repeat ' if user presses ESCAPE break out of the loop If KeyHit(KEY_ESCAPE) Exit ' if user presses DOWN ARROW move to next item in list If KeyHit(KEY_DOWN) link=link.NextLink() If link=Null link=mylist.FirstLink() selected$=String(link.Value()) EndIf ' if user presses UP ARROW move to previous item in list If KeyHit(KEY_UP) link=link.PrevLink() If link=Null link=mylist.LastLink() selected$=String(link.Value()) EndIf ' If DEL pressed remove selected item ' Now point to the first item in the remaining list ' If the list is empty exit from the loop If KeyHit(KEY_DELETE) mylist.Remove selected$ link=mylist.FirstLink() If link=Null Exit selected$=String(link.Value()) EndIf ' insert an entry AFTER the currently selected one If KeyHit(KEY_A) mylist.InsertAfterLink("Lemon "+Rand(100),link) EndIf ' insert an entry BEFORE the currently selected one If KeyHit(KEY_B) mylist.InsertBeforeLink("Lemon"+Rand(100),link) EndIf ' if user presses RETURN key show item selected If KeyHit(KEY_RETURN) Notify "Your choice is "+selected$ EndIf ' show list and highlight the 'selected' item Cls SetColor $88,$44,$00 DrawText "UP/DOWN = select item",10,10 DrawText "RETURN = confirm selection",10,24 DrawText "DEL = Remove selected item",10,38 DrawText "A = Insert 'Lemon' AFTER selected item",10,52 DrawText "B = Insert 'Lemon' BEFORE selected item",10,66 Local y=100 For Local l$=EachIn mylist SetColor $22,$22,$22 If l$=selected$ SetColor $ff,$ff,$ff DrawText l$,40,y y:+16 Next Flip Forever End it should be a tutorial !!! http://www.blitzbasic.com/Community/posts.php?topic=51996&hl=prevlink |
| ||
Good tutorial - very useful. Thanks |