I've seen some questions on this so I thought I could post this. Hope it helps! :)
; This demo shows the use of double or bi-directional LINK LISTS and CONTAINER CLASSES
; A LINK LIST is a common way of using pointers to string objects together
; each object will contain a pointer to the next object in the list and the previous object in the list
; The pointer to the first object in the list is often called te HEAD
; The previous pointer of the first object in the list will be NULL
; The next pointer of the the last object in the list will be NULL
; If the HEAD is the only object in the list both the previous and next pointers will be NULL
; If there are NO objects in the list then the HEAD pointer will be NULL
; A container class is a class that points to the object that we are working with as well as the next
; and previous objects in the LINK LIST. This allows and object to be part of many LINK LISTS and not
; just one, which is what would result if the DATA OBJECT (that is the main object that we are
; working with that contains the real data) contained the nxt/prv links.
; Check out the wikipedia.org artical on link lists for more info
; By Naphtali Moore, email me at NRMStudios at my yahoo.com account
Type Guild
Field Name$, Description$
Field head.gMember
End Type
Type gMember ; This is our container class
Field c.character
Field prv.gMember
Field nxt.gMember
End Type
Type character ;
Field Name$, HP, Def
End Type
curGuild.guild = new_Guild("Super Heros", "A group of powerful heros.") ; Create a Guild
; Create characters
p1.character = new_Character("Super Man", 100, 100)
p2.character = new_Character("Spider-Man", 40, 60)
p3.character = new_Character("Batman", 50, 75)
; Add characters to our guild
add_gMember(curGuild, p1)
add_gMember(curGuild, p2)
add_gMember(curGuild, p3)
; list the members
listMembers(curGuild)
;remove a member and list again
remove_gMember(curGuild, p2)
Print "": Print "Spider-Man has been removed." : Print ""
listMembers(curGuild)
;remove a member and list again
remove_gMember(curGuild, p1)
Print "": Print "Super Man has been removed." : Print ""
listMembers(curGuild)
;remove a member and list again
remove_gMember(curGuild, p3)
Print "": Print "Batman has been removed." : Print ""
listMembers(curGuild)
Print " "
Input("Please hit [ENTER] to end program.") ; wait for return to end
End
Function listMembers(g.Guild)
If g = Null Return
temp.gMember = g\head
Print "Guild Name: " + g\name
Print "========= MEMBER LIST ========="
If temp = Null Then Print "This guild has NO members."
While temp <> Null
i = i + 1
Print i + ") " + temp\c\name + " HP:" + temp\c\hp + " DEF:" + temp\c\def
temp = temp\nxt
Wend
End Function
Function new_character.character(name$, hp, def)
c.character = New character
c\name = name
c\hp = hp
c\def = def
Return c
End Function
Function new_Guild.Guild(name$, descrpt$)
g.Guild = New Guild
g\name = name
g\description = descrpt
Return g
End Function
Function add_gMember(g.guild, c.character)
If g = Null Or c = Null Then Return False ; This protects against errors
gM.gMember = New gMember ; create a new container object
gM\c = c ; assign character to container object
If g\head = Null Then ; if member list is empty
g\head = gM ; assign new object to first object of the list
Return True ; return possitive result
EndIf
temp.gMember = g\head ; get first object in list
While temp\nxt <> Null ; continue through list till you reach the last object
temp = temp\nxt ; move to next object
Wend
temp\nxt = gM ; point the last object to the new object
gM\prv = temp ; point the new object to the last object
Return True ; return positive result
End Function
Function remove_gMember(g.guild, c.character)
If g = Null If c = Null Then Return False ; This protects against errors
If g\head = Null Return False
temp.gMember = g\head
If g\head\c = c Then ; character is in first object of list
If g\head\nxt <> Null Then ; character is not only object in list
g\head = g\head\nxt ; make second object in list the first object in list
Else ; character is only object in list
g\head = Null
EndIf
Else
While temp\c <> c ; while object does not contain the character we are removing
temp = temp\nxt ; move to next object
If temp = Null Then Return False ; object not in list return negitive
Wend
If temp\nxt <> Null Then ; character is in middle of list
temp\prv\nxt = temp\nxt ; point the prv object to the nxt object
temp\nxt\prv = temp\prv ; point the nxt object to the prv object
Else ; character is in last object in list
temp\prv\nxt = Null ; make prv object last object in list
EndIf
EndIf
temp\c = Null ; remove pointer to our character
Delete temp ; delete the object containing our character
Return True ; return a positive result
End Function
|