Appending a list to a list?

Monkey Forums/Monkey Programming/Appending a list to a list?

Ken(Posted 2013) [#1]
I have this naive approach to appending one IntList onto the end of the other...but assuming that the list 'lhs' is hundreds of items long, am I copying and having to garbage collect the list each time I add an item?

Is there a built-in way to do this, (or a more elegant)?

Function appendList:IntList(lhs:IntList, rhs:IntList)
	For Local i:Int = Eachin rhs
		lhs.append(i)
	End
	return lhs
End
	



AdamRedwoods(Posted 2013) [#2]
You would have to essentially remove and re-add the Last lhsNode, and First rhsNode, using the Node Constructor "New Node(succ, prev, data)", because the succ and prev data changed for the tail and head for each list.


Ken(Posted 2013) [#3]
Not sure I follow..

If list lhs is ["a","b","c"], and rhs is ["e","f","g"], wouldn't stepping through rhs, and AddLast (not append as above) on lhs reliably get me an lhs of ["a","b","c","e","f","g"], and leave the rhs alone?

-Ken


AdamRedwoods(Posted 2013) [#4]
yes i should have clarified that your way above is good. you could substitute your "rhs.append" to "lhs.AddLast(i)". yes, you will incur some GC overhead, but if it's only a few hundred, that's minimal.

A stack may be a tad faster (for thousands of objects).

my way above would be convoluted and i'd have to think about actual implementation, but it would be fastest.