[futurebasic] Re: linked lists in FB

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 1997 : Group Archive : Group : All Groups

From: Chris <behmc@...>
Date: Thu, 4 Dec 1997 21:59:29 -0500
>On 12/4/97 at 9:00 AM, Chris <behmc@...> wrote:
>:Ok, I've been playing around with the idea of linked lists in FB and have
>:run into a snag.
>:I don't want to use global variables for this because a linked list is
>:supposed to be expandable and global structures are less expandable and
>:present another host of problems.  What I was trying to do is create a
>:global record that defined the list element structure.
>:DIM Head&                         'points to first element
>:DIM RECORD ListElement
>:  DIM dataH&
>:  DIM nextE&                      'points to the next element
>:DIM END RECORD .ListElement
>:Then I create a new handle with a list element's size
>:elementRef& = FN NEWHANDLE(8)
>So far, so good, though I'd do this in case you change your definition for
>ListElement later:
>elementRef& = FN NEWHANDLE(_ListElement)

Did this right after I sent the mail ;)

>:This is a handle to a chunk of memory 8 bytes long (enough to hold two 4
>:byte handles).
>:Then I create my first test record which is a ListElement record
>:DIM Test1.ListElement
>:This creates an 8 byte record.  Then I move the stuff from elementRef& into
>:the record (or at least i think i do)
>:Test1;8 = [elementRef&]
>What data was pointed to by [elementRef&]?  All you've done is deref the
>(leaving you with a pointer to two handles) and then *copy* the pointer to the
>variable Test1.

This is called it slipped my mind that by moving the data it was a copy and
then the original data wouldn't be accessed.  Looking at your example it
seems that you don't need to explicitly move the data into a record
structure, you can just apply that structure.  I recall, now, that the DIM
statements inside a record are turned into constants that basically define
offsets into the record (in FBII anyway).  That would explain why yours
works, you don't have to move the stuff into a DIMed structure :)

>:After that I assign Head&
>:Head& = @Test1
>Yuck.  Now Head& points to a pointer to two handles.
>Try this code to get an idea how to do linked lists in FB:

There's a reason that each element is just two handles though.  dataH& will
only store the address of something (function, record, something else, etc)
so that I can make the linked list totally generic (that's my hope anyway).

[example code snipped]

In your example code you had a new handle created each time you wanted to
add an element to the linked list.  if I had a dimmed variable that was
created in the image of the data structure (ie DIM someRec.ListElement) how
would I add that to the linked list?  Would I have to create a new handle,
put that handle into the nextElement of the tail (or wherever I'm adding it
to) and then move the data from the record into the handle?

Something like this:

DIM someRec.ListElement

LOCAL FN addToTail (aRec)
	newEle& = FN NEWHANDLE (_ListElement)

	[newEle&];8 = aRec
	'or would that be [[newEle&]];8 = aRec?

	'code to get to the tail
	'for arguments sake say that it is in tail&

	tail&..nextElement& = newEle&

Thanks again,