Jump to content
Sign in to follow this  
jruelan

LINKED LIST

Recommended Posts

jruelan

can autoit do linked list? and OOP?

Share this post


Link to post
Share on other sites
weaponx

You could fake it (maybe):

$string = "char firstname[128];char lastname[128];ptr next"

$head = DllStructCreate ($string)

DllStructSetData($head,1,"John")

DllStructSetData($head,2,"Smith")

DllStructSetData($head,3,"")

$headPtr = DllStructGetPtr ( $head)

$firstElement = DllStructCreate ($string)

DllStructSetData($firstElement,1,"John")

DllStructSetData($firstElement,2,"Smith")

DllStructSetData($firstElement,3,"")

DllStructSetData($head,3,DllStructGetPtr ($firstElement))

EDIT - changed last line

Edited by weaponx

Share this post


Link to post
Share on other sites
SkinnyWhiteGuy

You could fake it (maybe):

$string = "char firstname[128];char lastname[128];ptr next"

$head = DllStructCreate($string)

DllStructSetData($head,1,"John")

DllStructSetData($head,2,"Smith")

DllStructSetData($head,3,"")

$headPtr = DllStructGetPtr($head)

$firstElement = DllStructCreate($string)

DllStructSetData($firstElement,1,"John")

DllStructSetData($firstElement,2,"Smith")

DllStructSetData($firstElement,3,"")

DllStructSetData($head,2,DllStructGetPtr($firstElement))

I think you meant that last line to be

DllStructSetData($head,3,DllStructGetPtr($firstElement))

Otherwise, the first John Smith no longer has a last name.

Edit: As to OOP, that is not currently supported, see here.

Edited by SkinnyWhiteGuy

Share this post


Link to post
Share on other sites
weaponx

The problem with the above code is that in order to loop through a linked list you need to be able to read a structure by it's pointer, and although you can get a structures pointer, the DllStructGetData() function uses the handle of the structure to read from it and not the pointer.

Someone correct me if i'm wrong.

So i guess instead of storing a pointer to the next element perhaps you could store the autoit handle of the structure?

I tried this code below but I don't know what datatype the Autoit structure handle is so it doesn't work as expected:

;$string = "char firstname[128];char lastname[128];ptr next"

$string = "char firstname[128];char lastname[128];hwnd next"

$head = DllStructCreate ($string)

DllStructSetData($head,1,"John")
DllStructSetData($head,2,"Smith")
DllStructSetData($head,3,"")

;$headPtr = DllStructGetPtr ( $head)

$firstElement = DllStructCreate ($string)

DllStructSetData($firstElement,1,"Steve")
DllStructSetData($firstElement,2,"Jones")
DllStructSetData($firstElement,3,"")

;Assign next pointer by actual pointer
;DllStructSetData($head,3,DllStructGetPtr ($firstElement))

;Assign next pointer by autoit handle
DllStructSetData($head,3,$firstElement)

;Expecting: Steve
;Actual: 0
MsgBox(0,"",DllStructGetData(DllStructGetData($head,3),1))

;Display all elements
$nodePtr = $head
While $nodePtr <> ""
    ;Get next element
    MsgBox(0,"",DllStructGetData($nodePtr,1))
    $nodePtr = DllStructGetData($nodePtr,3)
WEnd

Share this post


Link to post
Share on other sites
weaponx

Does anyone know if there is a way to store the handle from DllStructCreate() in an already created structure? (i.e. what datatype is the internal AutoIt handle)

- or -

How can you use DllStructGetData() if you only have a pointer to a previously created structure?

Share this post


Link to post
Share on other sites
weaponx

Here is a working example of a linked list. I'm not sure how well it performs though. I thought I read somewhere that DllStruct* commands can be much slower than reading from an array for example. Credit goes to Valik for pointing out the mistakes in the previous post.

Local $string = "char firstname[128];char lastname[128];ptr next"

Local $head = DllStructCreate ($string)
Local $headPtr = DllStructGetPtr ( $head)

DllStructSetData($head,1,"First Name 1")
DllStructSetData($head,2,"Last Name 1")
DllStructSetData($head,3,0)

Local $firstElement = DllStructCreate ($string)

DllStructSetData($head, 3, DllStructGetPtr($firstElement))

DllStructSetData($firstElement,1,"First Name 2")
DllStructSetData($firstElement,2,"Last Name 2")
DllStructSetData($firstElement,3,0)

Local $pNext = $headPtr
While $pNext <> 0
    Local $current = DllStructCreate($string, $pNext)
    ConsoleWrite(DllStructGetData($current, 1) & @CRLF)
    ConsoleWrite(DllStructGetData($current, 2) & @CRLF)
    $pNext = DllStructGetData($current, 3)
WEnd

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×