Restored from previous forum. Originally posted by tinman.
Originally posted by pythagoras
Is this the good way of doing it ?
No. You have used pointers in some places where you should not. It's my fault, the documentation provided with the code isn't too great.
Here is an example showing how to use linked lists inside linked lists. The main thing to get right is the way you pass a pointer to the linked lists. But generally once you have done it correctly once then it never changes. If you have more questions please ask.
Code: Select all
xincludefile "double_linked_lists.pb"
; Simple structure for storing data about all characters
; or people (as an example)
Structure Character
c_Node.ListNode ; This must be the first item, and should be a regular variable, not a pointer
; Now you put all the data you want to store in the structure
; Only name for now since this is simple
c_Name.s ; Character name
EndStructure
; Simple structure for storing a list of gangs
Structure Gang
g_Node.ListNode ; Again, this must be first since each gang will be stored in a list
; Now your data comes here
g_Name.s ; Gang name
g_Hood.s ; Area of control
g_Members.ListHeader ; List for the members of the gang. Thie list must not be a pointer
EndStructure
DefType.ListHeader gang_list ; The main list for storing gangs
DefType.Gang *gang ; Pointer to a gang item, for working with list
DefType.Character *char ; Pointer to a character item, for working with lists
If OpenConsole()
PrintN(Str(dll_NewList(@gang_list, SizeOf(Gang) - SizeOf(ListNode))))
Repeat
Print("Enter gang name or leave blank to quit: ")
gang_name.s = Input()
PrintN("")
If gang_name""
; Gang name entered, create new item
*gang = dll_AddElement(@gang_list)
; make sure we only try to use it if successful
If *gang
*gang\g_Name = gang_name
Print("Enter area of control: ")
*gang\g_Hood = Input()
PrintN("")
; Create the list for members - you must use the full
; name to the lh_Head field due to a bug in PB you cannot
; simply use the g_Member item.
dll_NewList(@*gang\g_Members\lh_Head, SizeOf(Character) - SizeOf(ListNode))
; Now we add the gang members, in a similar way to
; when we create the gangs
Repeat
Print(" Enter member name or leave blank to stop: ")
member_name.s = Input()
PrintN("")
If member_name""
*char = dll_AddElement(@*gang\g_Members\lh_Head)
If *char
*char\c_Name = member_name
EndIf
EndIf
Until member_name=""
EndIf
EndIf
Until gang_name=""
; Now we display the contents of the lists to show you it worked
*gang = gang_list\lh_Head
While *gang
PrintN("The gang '"+*gang\g_Name+"' controls "+*gang\g_Hood+". Members are:")
*char = *gang\g_Members\lh_Head
While *char
Debug *char
PrintN("foo")
PrintN(" "+*char\c_Name)
*char = *char\c_Node\ln_Succ
Wend
PrintN("End of members")
; Move onto next item
*gang = *gang\g_Node\ln_Succ
Wend
PrintN("Freeing memory")
; Call this to free up memory in each list
; First we go to the first item in the gangs
*gang = gang_list\lh_Head
; And go round this loop until the pointer is null (end of gang list)
While *gang
; Clear the list for members of the gang
; Must use the @ symbol to get the address of the start
; of the list header inside the gang item structure
dll_DeleteList(@*gang\g_Members\lh_Head)
; Move onto next item
*gang = *gang\g_Node\ln_Succ
Wend
; Finally we must clear the main list
dll_DeleteList(@gang_list)
PrintN("Press return to exit")
Input()
CloseConsole()
EndIf
End
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + all updates, PB3.51, Ed3.53)