The 2 last lines displayed by the debugger should be 777 and 9999
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Linked list example file
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
Structure BasicStructure
Field1.b
Field2.w
Field3.l
EndStructure
Structure ComplexStructure
Field1.b
*Basic.BasicStructure ; Pointer to a BasicStructure object
Basic2.BasicStructure ; Creation of the BasicStructure object inside this structure
*Next.ComplexStructure ; Pointer to another ComplexStructure object
EndStructure
NewList TestList.BasicStructure()
NewList TestComplexList.ComplexStructure()
;
;-------- Add Elements and TestLists --------
;
AddElement(TestList())
TestList()\Field2 = 1
AddElement(TestList())
TestList()\Field2 = 2
AddElement(TestList())
TestList()\Field2 = 3
AddElement(TestList())
TestList()\Field2 = 4
Debug "Number of elements in the list: " + ListSize(TestList())
; First way to list all the elements
;
ResetList(TestList()) ; Reset the list index before the first element.
While NextElement(TestList()) ; Process all the elements...
Debug "ResetList() - 'Field2' value: " + TestList()\Field2
Wend
; Second way, with the help of ForEach
;
ForEach TestList() ; Process all the elements...
Debug "ForEach() - 'Field2' value: " + TestList()\Field2
Next
SelectElement(TestList(), 2) ; Go directly to the 3rd element
Debug "3rd Element - 'Field2' value: " + TestList()\Field2
Debug ""
; ComplexStructure
;
;-------- Add Elements and TestLists --------
;
AddElement(TestComplexList())
TestComplexList()\Field1 = 11
AddElement(TestList())
TestComplexList()\Basic = AllocateStructure(BasicStructure)
TestComplexList()\Basic\Field1 = 111 ; It's a pointer but we don't use the * (it's a PureBasic oddity)
TestComplexList()\Basic\Field2 = 222
TestComplexList()\Basic\Field3 = 333
AddElement(TestList())
TestComplexList()\Basic2\Field1 = 1111
TestComplexList()\Basic2\Field2 = 2222
TestComplexList()\Basic2\Field3 = 3333
AddElement(TestList())
TestComplexList()\Next = AllocateStructure(ComplexStructure)
TestComplexList()\Next\Field1 = 7
TestComplexList()\Next\Basic = AllocateStructure(BasicStructure)
TestComplexList()\Next\Basic\Field1 = 77
TestComplexList()\Next\Basic\Field2 = 88
TestComplexList()\Next\Basic\Field3 = 99
TestComplexList()\Next\Basic2\Field1 = 777
TestComplexList()\Next\Basic2\Field2 = 888
TestComplexList()\Next\Basic2\Field3 = 999
TestComplexList()\Next\Next = AllocateStructure(ComplexStructure)
TestComplexList()\Next\Next\Field1 = 9999
; Debug several fields
Debug TestComplexList()\Field1 ;= 11
Debug TestComplexList()\Basic\Field2 ;= 222
Debug TestComplexList()\Basic2\Field3;= 3333
Debug TestComplexList()\Next\Field1 ;= 7
Debug TestComplexList()\Next\Basic\Field1 ;= 77
Debug "WRONG UNDER"
Debug TestComplexList()\Next\Basic2\Field1 ;= shoud be 777 <=== WRONG
Debug TestComplexList()\Next\Next\Field1 ;= shoud be 9999 <=== WRONG
End
M.

