Page 1 of 1

Pointer to a linked list?

Posted: Mon Jun 28, 2004 8:16 pm
by matthew180
Greetings,

Is it possible to have a pointer to a linked list? I have a function that performs an operation that could act on any one of an unknown number of lists. I tried this, but got an error: Syntax error in Procedure arguments.

Code: Select all

Procedure do_list(*list())
    ResetList(*list())

    While NextElement(*list())
        ; Do operation
    Wend
EndProcedure
Any insight would be greatly appreciated.

Thanks,
Matthew

Posted: Mon Jun 28, 2004 9:03 pm
by tinman
Not as far as I know, but it would be something I'd love to see. I couldn't figure out how to do a semi-nasty hack to get the list base because the links in the elements point to NULL at either end of the list rather than back to the list header.

However, you could probably do some really full on nasty hack and and do something like this, which works but its a hack:

Code: Select all

Structure MyType
    a.l
EndStructure


Structure nodeMyType
    *next.nodeMyType
    *prev.nodeMyType
    dat.MyType
EndStructure

Structure ListHead
    *head.nodeMyType
    *tail.nodeMyType
EndStructure


DefType.ListHead    *lh
DefType.nodeMyType  *node
NewList mylist.MyType()

If OpenConsole()

    If AddElement(mylist())
        mylist()\a = 666
    EndIf


    PrintN("Address of data in first element = 0x"+RSet(Hex(@mylist()), 8, "0"))
    PrintN("Address of structure of first element = 0x"+RSet(Hex(@mylist()-8), 8, "0"))

    !MOV eax,[e_mylist]
    !MOV dword [p_lh],eax
    PrintN("Current element of list = 0x"+RSet(Hex(*lh), 8, "0"))

    !MOV eax,[t_mylist]
    !MOV dword [p_lh],eax
    PrintN("1. Base address of list = 0x"+RSet(Hex(*lh), 8, "0"))
    PrintN("1. Head pointer in list = 0x"+RSet(Hex(*lh\head), 8, "0"))
    PrintN("1. Tail pointer in list = 0x"+RSet(Hex(*lh\tail), 8, "0"))

    ; Prove it
    If AddElement(mylist())
        mylist()\a = 123
        PrintN("Address of new element (data/structure) = 0x"+RSet(Hex(@mylist()), 8, "0")+"/"+RSet(Hex(@mylist()-8), 8, "0"))
        PrintN("2. Head pointer in list = 0x"+RSet(Hex(*lh\head), 8, "0"))
        PrintN("2. Tail pointer in list = 0x"+RSet(Hex(*lh\tail), 8, "0"))
    EndIf

    ResetList(mylist())
    While NextElement(mylist())
        PrintN("A. data = "+Str(mylist()\a))
    Wend
    
    PrintN("")
    
    *node = *lh\head
    While *node
        PrintN("B. data = "+Str(*node\dat\a))
        *node = *node\Next
    Wend

    PrintN("Press return to exit")
    Input()
    CloseConsole()
EndIf
End

Posted: Mon Jun 28, 2004 9:17 pm
by matthew180
That's what I am finding out (that you can't.) That's too bad because it really limits the usefulness of Lists since there are no built in searching or sorting routines for Lists... And since you can't pass a List by reference, it makes writing a generic List search and/or sort procedure impossible.

My particular project is small (this time), but it would really be a pain in a larger application.

Thanks for the info!

Matthew

Posted: Mon Jun 28, 2004 9:29 pm
by tinman
matthew180 wrote:That's what I am finding out (that you can't.) That's too bad because it really limits the usefulness of Lists since there are no built in searching or sorting routines for Lists...
Well you can always pass the address of the first or last element of the list to any procedures that you write? That's what I ended up having to do when I implemented my sort routines for linked lists.
matthew180 wrote:And since you can't pass a List by reference, it makes writing a generic List search and/or sort procedure impossible.
The code I just posted shows you how to get the list base pointer and use that. So if you are into hacks then you can pass lists by reference :)

There was a thread in the feature requests which spoke about procedure parameters and it sounded like Fred was going to add direct support for arrays and lists as parameters. Obviously I don't know when that would be added though.

Posted: Tue Jun 29, 2004 9:12 am
by DarkDragon
You can Point to a LinkedList(with a '@'), but between the Elements are other bytes.

Posted: Tue Jun 29, 2004 11:36 am
by GedB
You might find the following thread helpful:

viewtopic.php?t=11219&highlight=

Posted: Tue Jun 29, 2004 11:38 am
by GedB
Oh, and LinkedList and Array paramaters are on the way:

viewtopic.php?t=11389