linked lists

BlitzMax Forums/BlitzMax Beginners Area/linked lists

Richard B(Posted 2005) [#1]
Silly question (searched high and low and can't find anything), but does the built in linked list library have a way of getting the first and/or last member of the list? Something like

myList:TList = new Tlist

listAddLast myList, "one"
listAddLast myList, "two"
listAddLast myList, "three"

' I want to do something like

a$ = myList.first ' ie get the first member in the list

'or

a$ = myList.last ' ie get the first member in the list


I am trying to avoid stepping through the whole list with EachIn just to get the last member.

thanks,
Richard.


Perturbatio(Posted 2005) [#2]
ahem!

nothing to see here, move along.


Perturbatio(Posted 2005) [#3]
Oi! I told you to move along! So do it dammit!


Bot Builder(Posted 2005) [#4]
Umm

TList.First()
TList.Last()


Perturbatio(Posted 2005) [#5]
oops :)
I skimmed the source twice and missed them both times :)


Richard B(Posted 2005) [#6]
Thanks Bot - just what I was after - couldn't find it in the docs anywhere.

Richard.

PS What about "tList.next()" and "tList.prev()"?


Perturbatio(Posted 2005) [#7]
Now that *would* require you to extend the type (or manually access the _pred and _succ properties of the Link).


Dreamora(Posted 2005) [#8]
No extension needed, just usage of OO is needed as the none OO part does not contain this functionality ( which is why it isn't mentioned in the docs )


tonyg(Posted 2005) [#9]
How could the Prevlink and Nextlink methods be used?
I still struggle with converting tlinks into something tangible.


Perturbatio(Posted 2005) [#10]
with a TLink, you can typecast the _value field.
i.e. string(myTLink._value) for string


Richard B(Posted 2005) [#11]
Can you please elaborate - how can I get the next/prev member of the tList.

many thanks,
Richard.


Perturbatio(Posted 2005) [#12]
Local myList:TList = New TList
	myList.AddLast("test1")
	myList.AddLast("test2")
	myList.AddLast("test3")
	myList.AddLast("test4")
	myList.AddLast("test5")

Local current:TLink = myList.FirstLink()
Print String(current._value)
current = current._succ

While current <> myList._head
	Print String(current._value)
	current = current._succ
Wend 



Richard B(Posted 2005) [#13]
Great! - many thanks,
Richard