Linked list tweak gone arry
BlitzMax Forums/BlitzMax Module Tweaks/Linked list tweak gone arry
| ||
I've been looking att he best way to organize my parsed data, and put it through various processes to change it, and this seems like the best way - have a list of objects. In many cases, obviously, the objects will have inter-relationships, usually before/after/etc. Decided the best way to do this was access the last created enumerator... Anyway, in working on it ive noticed some odd stuff:Strict
Framework brl.standardio
Local hmm:TList=New TList
hmm.AddLast("moo")
hmm.Addlast("cow")
For Local i$=EachIn hmm
Print i$
hmm.Enum._link._value="hi"
Next
Type TLink
Field _value:Object
Field _succ:TLink,_pred:TLink
End Type
Type TListEnum
Field _link:TLink
Method HasNext()
Return _link._value<>_link
End Method
Method NextObject:Object()
Local value:Object=_link._value
Assert value<>_link
_link=_link._succ
Return value
End Method
End Type
Type TList
Field _head:TLink
Field Enum:TListEnum
Method New()
_head=New TLink
_head._succ=_head
_head._pred=_head
_head._value=_head
End Method
Method Delete()
_head._value=Null
_head._succ=Null
_head._pred=Null
End Method
Method AddLast:TLink( value:Object )
Return InsertBeforeLink( value,_head )
End Method
Method InsertBeforeLink:TLink( value:Object,succ:TLink )
Local link:TLink=New TLink
link._value=value
link._succ=succ
link._pred=succ._pred
link._pred._succ=link
succ._pred=link
Return link
End Method
Method ObjectEnumerator:TListEnum()
enum=New TListEnum
enum._link=_head._succ
Return enum
End Method
End TypeThis is some example code plus a very cut-down linked list module. as you can see very oddly it loops. I dont see why it would. By setting the value of the current node, it should simply overwrite it and move on (a flushmem would cleanup the leftovers). However, for whatever reason, it loops forever instead. I've tried debugstop, but for some reason or another debugstop basically exits the program for me. never used it before. I hit step and it skips everything o.0, in otherwords i see nothing in output. Posting it here cause it has code from a brl module... Any idea whats going on? If i comment out the thing that is only changing the value of a variable it works fine, but obviously doesnt change the stuff in the list. Sure, I could make a new list but its inefficient and less elegant. |
| ||
| figured it out - the linked list is actually a linked loop, where the end/start has a value of TLink. To obtain the actual current link you have to access the previous one, which is what causes it to overwrite the head with a string causing an infinite loop. |