Pointer to a linked list?

Everything else that doesn't fall into one of the other PB categories.
matthew180
User
User
Posts: 64
Joined: Mon Jun 30, 2003 5:36 pm
Location: Michigan
Contact:

Pointer to a linked list?

Post 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
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
matthew180
User
User
Posts: 64
Joined: Mon Jun 30, 2003 5:36 pm
Location: Michigan
Contact:

Post 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
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

You can Point to a LinkedList(with a '@'), but between the Elements are other bytes.
bye,
Daniel
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

You might find the following thread helpful:

viewtopic.php?t=11219&highlight=
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Oh, and LinkedList and Array paramaters are on the way:

viewtopic.php?t=11389
Post Reply