No announcement yet.

Linked list of Objects

  • Filter
  • Time
  • Show
Clear All
new posts

  • Linked list of Objects

    OK, I've used linked lists before, and I've used Objects. But I've never put them together.

    I'm trying to create a linked list (forward and backward) of objects. Each Object of course has a pair of Forward/Backward pointers to the Next/Previous items.

    But I just cannot figure out how to swap these pointers when doing functions like adding or removing items from the list.

    The Prev/Next pointers are simply Instance variables defined as
    INSTANCE NEXT as iName
    For example, If I'm inserting a new item X to go between items C and D

    It I try
    X.Next = C.Next
    to pick up the previous pointer I get Error 595 - Object return type required.

    What am I overlooking here?


  • #2
    George, there are examples of linked lists here, but have you considered using a PowerCollection?


    • #3
      Chris: PowerCollection I have used, but it's a key/data association, which doesn't exist in my case. I've used link lists many times, so I'm not looking for examples on that aspect. What I cannot figure out is how to manipulate the Prev/Next pointers that exist within each list item, when they are Objects.

      I've tried manually building a 2 item list with valid Fwd/Back chains. Works fine.

      When I try and insert a new Object item to the list between the two, it requires copying the Fwd/Back pointers between items to insert the new item into the chains. It seems PB just does not like the syntax of objvar1.instancename1 = objvar2.instancename1 when the instance names are declared as Objects.

      I've been looking in Help and see there's something called OBJPTR, that may be what I'm after as they're stored as DWORDS, which wouldn't cause the error I'm seeing. But then how I use OBJPTRs? Ah! More reading methinks.




      • #4
        Right, I think the assignment may work if done with a variable in the middle, as in l = = l.

        If you were to use a PowerCollection, the value part of your collection would be the object, and the key, the timestamp at the point of insertion, for example, if there is no other implicit key. The advantage of PowerCollection is that the navigation is all in place, you can traverse the collection with next/previous, etc.

        Come to think of it, there is a linked list type of collection too,
        The CLASS is "LinkListCollection". The INTERFACE is ILinkListCollection (a DUAL interface).


        RetVal = <ObjectVar>.membername(params)

        <ObjectVar>.membername(params) TO ReturnVariable
        What type of interface are you using, by the way? Unknown, Dispatch, etc


        • #5
          I'm just using iUnknown interface. I think the problem with using PowerCollection/LinkList is that my access to the data is limited. I have to retrieve the entire object in order to examine or change any single instance variable.

          I found some help with using OBJPTR from Jose. He posted some code to reverse an OBJPTR to a an Object. I'm playing with this right now. I was also thought of trying a UNION to define the Prev/Next pointers which might allow a bypass as well. Not sure till I try it whether UNION will support an Object variable.



          • #6
            Well, UNIONS are no help, they don't support Object variables. OBJPTR I couldn't get to function, even with Jose's Ptr2Obj code. I got crashes I just couldn't figure out.

            So, I went with a dynamic array of objects to act as a resource pool, and using the array index as the prev/next pointers. This at least is working and so far hasn't made for anything awkward in the code. It would still be more intuitive to have the Prev/Next pointers be actual Object pointers, but ........



            • #7
              If you can live with the OBJPTR() as a value, here is some double-linked list code free for the download......

              Dynamic Allocation and Linked Lists for both PB/DOS and PB/CC
              Public Domain, as appeared in April 1999 issue of BASICally Speaking

              For Gary Beene: This is what you get when you don't rely on third-party programs or specific versions of the O/S... code which still works after 19+ years!