回复 25# seeyou #PRE_UseX64=n
Const $tagNode = 'int id;ptr next'
Local $aNode[4] = [DllStructCreate($tagNode), DllStructCreate($tagNode), DllStructCreate($tagNode), DllStructCreate($tagNode)]
For $i = 0 To 3
DllStructSetData($aNode[$i], 'id', $i+1)
If $i < 3 Then
DllStructSetData($aNode[$i], 'next', DllStructGetPtr($aNode[$i + 1]))
EndIf
Next
;至此形成一个单向表,$aNode[0]->next = $aNode[1],$aNode[1]->next = $aNode[2],$aNode[n]->next = $aNode[n+1]
;但最后一个节点无指向,为0(NULL),因为DllStructCreate会在内部进行被始化
Local $pList = DllStructGetPtr($aNode[0]);表头,这里假设上面的代码全是不可见的,只知道链表表头的地址,有多少个节点我们也不知道
;问题:已知链接表头为$pList,要求输出节点中的id成员为3的下一个节点中的id成员的值
;解答:
Local $tBuffer
While ($pList)
$tBuffer = DllStructCreate($tagNode, $pList);以 $tagNode 结构形式来读取$pList,这里$pList是一个真实的内存地址
If DllStructGetData($tBuffer, 'id') = 3 Then;if(pList->id == 3)
$pList = DllStructGetData($tBuffer, 'next');pList = pList->next
If $pList Then;if(pList)
$tBuffer = DllStructCreate($tagNode, $pList)
MsgBox(64,'Done',StringFormat('下一个节点的ID成员值为:%d',DllStructGetData($tBuffer,'id')));pList->id
ExitLoop
EndIf
EndIf
$pList = DllStructGetData($tBuffer, 'next')
WEnd
;验证:
Local $sMsg = ''
For $i = 0 To 3
$sMsg &= StringFormat('节点:%d 的id成员值:%d\n',$i+1,DllStructGetData($aNode[$i],'id'))
Next
MsgBox(64,'验证结果',$sMsg)
|