Page 2 sur 2
Publié : jeu. 21/juil./2005 8:42
par Le Soldat Inconnu
Moi, pour faire ce genre de chose dans un code, je serais passer par une seule liste chainée avec un index qui différencie chaque liste stockée dedans, je m'explique :
j'aurai fait une Structure de ce type
Structure
Index.l
...
Le reste
...
EndStructure
Et après, suivant la liste qu'on souhaite, on recherche l'index que l'on souhaite.
Certainement plus lent au niveau code, mais un bon apprentissage pour ceux qui aime la bidouille

et on n'a pas besion de connaitre les pointeurs. Si ça peut donner des idées d'algo

Publié : jeu. 21/juil./2005 12:19
par VPureBasic
Salut Nico,
Je suis du meme avis que Le Soldat Inconnu. Je t'ai coder un petit exemple... pas parfait... mais surement un bon depart.
Code : Tout sélectionner
Structure NODE
*NextNode.NODE
*PrevNode.NODE
NumberID.l
EndStructure
Structure LIST
*HeadNode.NODE
*TailNode.NODE
*PredNode.NODE
*StopNode.NODE
SizeNode.l
NodePads.l
EndStructure
Structure MASTER_LIST
NumberID.l
ListAddr.l
EndStructure
NewList LIST_MasterList.MASTER_LIST()
; Fontion interne
Procedure.l LIST_GetAddress( ListID.l )
;
ResetList( LIST_MasterList() )
While NextElement( LIST_MasterList() )
If LIST_MasterList()\NumberID = ListID
ProcedureReturn LIST_MasterList()\ListAddr
EndIf
Wend
ProcedureReturn 0
;
EndProcedure
Procedure.l LIST_GetElement( *ListAddr.LIST,ElementID.l )
;
*ReadNode.NODE = *ListAddr\HeadNode
*StopNode.NODE = *ListAddr\StopNode
;
While *ReadNode <> *StopNode
If *ReadNode\NumberID = ElementID
ProcedureReturn *ReadNode
EndIf
*ReadNode = *ReadNode\NextNode
Wend
ProcedureReturn 0
;
EndProcedure
Procedure LIST_InitList ( *ListAddr.LIST,NodeSize.l )
;
*ListAddr\PredNode = *ListAddr
*ListAddr\HeadNode = *ListAddr + 4
*ListAddr\StopNode = *ListAddr + 4
*ListAddr\SizeNode = NodeSize
*ListAddr\TailNode = 0
*ListAddr\NodePads = 0
;
EndProcedure
Procedure LIST_ClearItem ( *NodeAddr.NODE )
;
*NextNode.NODE = *NodeAddr\NextNode
*PrevNode.NODE = *NodeAddr\PrevNode
;
*NextNode\PrevNode = *PrevNode
*PrevNode\NextNode = *NextNode
;
FreeMemory( *NodeAddr )
;
EndProcedure
Procedure LIST_ClearList ( *ListAddr.LIST )
;
While *ListAddr\PredNode <> *ListAddr
LIST_ClearItem( *ListAddr\PredNode )
Wend
;
EndProcedure
;
; fonctions de travail
Procedure LIST_DelElement( ListID.l,ElementID.l )
;
*ListAddr.LIST = LIST_GetAddress(ListID)
If *ListAddr <> 0
*NodeAddr.NODE = LIST_GetElement( *ListAddr,ElementID)
If *NodeAddr <> 0
LIST_ClearItem( *NodeAddr )
*ListAddr\NodePads = *ListAddr\NodePads - 1
EndIf
EndIf
;
EndProcedure
Procedure.l LIST_AddElement( ListID.l,ElementID.l )
;
If ElementID >= 0
If ListID >= 0
;LIST_DelElement( ListID,ElementID )
;
*ListAddr.LIST = LIST_GetAddress( ListID )
If *ListAddr <> 0
*NodeAddr.NODE = AllocateMemory( *ListAddr\SizeNode )
If *NodeAddr <> 0
*PrevNode.NODE = *ListAddr\PredNode
*PrevNode\NextNode = *NodeAddr
*NodeAddr\PrevNode = *PrevNode
*NodeAddr\NextNode = *ListAddr + 4
*NodeAddr\NumberID = ElementID
*ListAddr\PredNode = *NodeAddr
*ListAddr\NodePads = *ListAddr\NodePads + 1
ProcedureReturn *NodeAddr
EndIf
EndIf
EndIf
EndIf
ProcedureReturn 0
;
EndProcedure
Procedure LIST_DeleteList( ListID.l )
;
If ListID >= 0
ListAddr = LIST_GetAddress(ListID)
;
If ListAddr <> 0
LIST_ClearList( ListAddr )
DeleteElement( LIST_MasterList() )
EndIf
;
EndIf
;
EndProcedure
Procedure.l LIST_CreateList( NumberID.l,NodeSize.l )
;
If ListID >= 0
LIST_DeleteList(ListID)
;
If AddElement( LIST_MasterList() )
ListAddr = AllocateMemory(SizeOf(LIST))
;
If ListAddr <> 0
LIST_MasterList()\NumberID = ListID
LIST_MasterList()\ListAddr = ListAddr
LIST_InitList(ListAddr,NodeSize)
ProcedureReturn ListAddr
EndIf
;
EndIf
EndIf
ProcedureReturn 0
;
EndProcedure
Roger
La seule limite du PureBasic... c'est notre imagination!