Forum Navigation
You need to log in to create posts and topics.

Re: linked lists in FB

Posted by: Greg_Neagle <Greg_Neagle@...>

On 12/4/97 at 9:00 AM, Chris <behmc@alleg.edu> 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)

: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 handle
(leaving you with a pointer to two handles) and then *copy* the pointer to the
variable Test1.

: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:
'-------------------------------------

DIM head& 'top of linked list

DIM RECORD myListElement
DIM 32 theName$
DIM theValue%
DIM theSquaredValue%
DIM nextElement&
DIM END RECORD.myListElement

DIM element&, x

element& = FN NEWHANDLE(_myListElement)
head& = element& 'set head& to top of list (first element)

FOR x = 1 TO 10
element&..theName$ = "Element " + STR$(x)
element&..theValue% = x
element&..theSquaredValue% = x*x
LONG IF x < 10
element&..nextElement& = FN NEWHANDLE(_myListElement)
element& = element&..nextElement&
XELSE
element&..nextElement& = 0
END IF
NEXT x

WINDOW 1
PRINT "Name", ,"Value", "Squared" 'print column headers
element& = head& 'set element& to the first
element of the list
DO
PRINT element&..theName$, ,element&..theValue%,
element&..theSquaredValue%'print values from this element
element& = element&..nextElement& 'get next element
UNTIL element& = 0 'if it's zero we're done.

PRINT
PRINT "Click to exit."

DO
UNTIL FN BUTTON

- via BulkRate 2.6.x