Linked List Problem

BlitzMax Forums/BlitzMax Programming/Linked List Problem

Chroma(Posted 2007) [#1]
Here's my dilemna. I have a type and two of the fields are TLists. I need them to talk to each other so when I change a value in one list, it changes in the other. I need to keep them non-global and in the Type.

Some code example:
Type Blah
     Field List1:TList = New TList
     Field List2:TList = New TList
End Type



Picklesworth(Posted 2007) [#2]
Could you have a method inside Type Blah that handles the various list operations?

It's good to do that, anyway. I forget what the term is, but one thing done with OOP is to have methods to change any variables in a Type. That way you can make changes to how data is stored fairly easily, looking nowhere except for in the Type definition.


Chroma(Posted 2007) [#3]
Hmm...testing.

I think I'm a victim of my own complexity, if that makes sense.

This sucks...If I have global lists I can change object data in one list and it affects the other. But I can't figure out how to do that with the above configuration. :(


Chroma(Posted 2007) [#4]
Ugh...I found the bug. A line I put in for testing and I even commented that I needed to take it out...


bradford6(Posted 2007) [#5]
can you post your solution?


Chroma(Posted 2007) [#6]
Sure. Remember that TBlahs are stored in List1 and List2.
Type TSomething
     Field List1:TList = New TList
     Field List2:TList = New TList
End Type


Local this:TBlah
For this = Eachin List2
     If this.value = 100
          KillThisBlahInList1( this )
          self.List2.Remove( this )
     Endif
Next
...

Method KillThisBlahInList1( that:TBlah )
     Local this:TBlah
     For this = Eachin List1
          If this = that
               self.List2.Remove( this )
               Exit     (or Return False)
          Endif
     Next
End Method



Brucey(Posted 2007) [#7]
Can't you simply do this ?
Method KillThisBlahInList1( that:TBlah )
    self.List1.Remove(that)
End Method


:-)


Dreamora(Posted 2007) [#8]
yes. especially the above would be a total overkill. remove will loop through the list anyway so looping to find the object and then call remove on the list instead of a tlink means twice looping for the same thing


bradford6(Posted 2007) [#9]
Do the 2 lists contain exactly the same objects?