Link list

Archives Forums/BlitzPlus Programming/Link list

Jesse(Posted 2005) [#1]
Can any body help me? can Somebody provide me with a sample of a link-list if possible? I knew how to use them in c but I have not used them in B+. so I don't know it this is possible.


Grey Alien(Posted 2005) [#2]
I thought there was an example in the blitz docs or the samples folder, but I'm at work right now nd can't check. Or it might have been on Blitz Coder. Also check the code archives on this this.


Russell(Posted 2005) [#3]
Apparently, lists are handled a bit differently now. They use Types. Here's an example from the manual:
format_code('
; Define a crafts Type

Type crafts
Field x
Field y
Field dead
Field graphic
End Type

; Create 100 crafts, with the unique name of alien
For t = 1 To 100
alien.crafts = New crafts
alien\x = Rnd(0,640)
alien\y = Rnd(0,480)
alien\dead = 0
alien\graphic = 1
Next

; Move to the first object
alien.crafts = First crafts

Print alien\x
Print alien\y
Print alien\dead
Print alien\graphic

; move to the next alien object
alien = After alien

Print alien\x
Print alien\y
Print alien\dead
Print alien\graphic

; move to the last alien object
alien.crafts = Last crafts

Print alien\x
Print alien\y
Print alien\dead
Print alien\graphic

; move to the second to the last alien object
alien = Before alien

Print alien\x
Print alien\y
Print alien\dead
Print alien\graphic
')
There doesn't seem to be a NewList command et al like before... Just the syntax has changed I guess. All the same functionality is there, though.

Russell


Grey Alien(Posted 2005) [#4]
You can still make a linked list where one object points to the next in the list and if you delete one you must alter the previous one in the list etc. I don't have a use for them in my game at the moment, but for windows apps. they are pretty useful, things like StringLists for example.


Jesse(Posted 2005) [#5]
Thanks a lot guys. That was helpfull. I am creating a game in which unlimited parts and weapons of differnt sort are aded to vehircles. a child pointer for weapons and a child pointer for parts. of course all inclusive for the same parent.
... I created the program below in c and was able to convert it to B+. by looking at some samples. It is not thouroughly documented but it is fairly easy to understand.

AppTitle "Test Engine"

; menu constants for event processing

Const FILELOAD=1
Const DISPLAYFILE=2
Const FILESAVE=3
Const FILEQUIT=4
Const HELPABOUT=5
Const REMOVEFILE=6
Const BACKGROUND= 1



Global box,test,testactive,decoding
Global filename$
testactive = False
decoding = False

Type file
Field strng$
End Type

Type option
Field included$
End Type

Type element
Field Question$
Field firstchoice.option
Field lastchoice.option
Field firstanswer.option
Field lastanswer.option
Field selected
Field correct
End Type

; open a window so we can show off our menu

window=CreateWindow("A+ test review",40,40,600,600)

; create the menu

menu=WindowMenu(window)
file=CreateMenu("File",0,menu)
CreateMenu "Load",FILELOAD,file
CreateMenu "&Display",FILENEW,file
CreateMenu "Save",FILESAVE,file
CreateMenu "Close",REMOVEFILE,file
CreateMenu "Quit",FILEQUIT,file

help=CreateMenu("Help",0,menu)
CreateMenu("About",HELPABOUT,help)
UpdateWindowMenu window
box=CreateTextArea (0,0,GadgetWidth(window)-20,GadgetHeight(window)-70,window)
SetTextAreaColor(box,0,0,0,BACKGROUND) ; background
SetTextAreaColor(box,255,255,0)
Repeat
Select WaitEvent()
Case $1001 ;menu event
Select EventData()
Case FILELOAD:

If testactive Then
filename$=RequestFile("Load Practice test","txt")
If Len(filename$) Then
Deleteoldtest
loadtest
EndIf
Else
filename$=RequestFile("Load Practice test","txt")
If Len(filename$) Then
loadtest
EndIf
EndIf
Case DISPLAY:
showfile
Case REMOVEFILE:
If testactive Then
Deleteoldtest
Else
Notify "No file to delete"
EndIf

Case FILESAVE:
filename$=RequestFile("Save File",filename$,True)
Case FILEQUIT:
End
Case HELPABOUT
Notify "selecting About = user interest"
End Select

Case $803: ;close window event
deleteoldtest
End
End Select
Forever

Function loadtest()
test = ReadFile(filename$)
If test Then
While Not Eof(test)
raw$ = ReadLine(test)
Trim$(raw$)
If Len(raw$) Then
If Left$(raw$,1)= "~" Then
current.element = New element
current\firstchoice.option = Null
current\lastchoice.option = Null
current\firstanswer.option = Null
current\lastanswer.option = Null
current\question$ = Replace$(raw$,"~","")
testactive = True
Else
If Left$(raw$,1) >= "A" And Left$(raw$,1)<="Z" Then
temp.option = New option
If current\firstchoice = Null Then
current\firstchoice.option = temp.option
current\lastchoice.option = temp.option
EndIf
temp\included$ = raw$
Insert temp.option After current\lastchoice.option
current\lastchoice.option = temp.option
EndIf
If Left$(raw$,1) = "@" Then
temp.option = New option
If Current\firstanswer.option = Null Then
current\firstanswer.option = temp.option
current\lastanswer.option = temp.option
EndIf
temp\included$ = Replace$(raw$,"@","")
Insert temp.option After current\Lastanswer.option
current\lastanswer.option = temp.option
EndIf
EndIf

EndIf
Wend
CloseFile(test)
Else
Notify"Error: file not found"

EndIf
End Function

Function showfile()
SetTextAreaText(box,"",0,TextAreaLen(box),2)
If testactive Then

For current.element = Each element

AddTextAreaText(box, Chr$(13)+current\question$+Chr$(13))
temp.option = current\firstchoice.option
While temp.option <> Null
AddTextAreaText(box, temp\Included$+Chr(13))
If temp.option= current\lastchoice.option Then Exit
temp.option = After temp.option
Wend
temp.option = current\firstanswer.option
While temp.option<>Null
AddTextAreaText(box,temp\included$+Chr$(13))
If temp.option= current\lastanswer.option Then Exit
temp.option = After temp.option
Wend



Next

Else
Notify("Load a file first",1)
EndIf
End Function

Function deleteoldtest()
If testactive Then
SetTextAreaText(box,"",0,TextAreaLen(box),2)
For current.element = Each element
temp.option = current\firstchoice.option
For temp.option = Each option
Delete temp
Next
temp.option = current\firstanswer.option
For temp.option = Each option
Delete temp
Next
Delete current
Next
testactive =0
EndIf


Kevin_(Posted 2005) [#6]
The conventional way to build a linked list is to put all your values in an array then create a seperate array full of pointers.

Take the List n...

4
7
2
5
1

Now look at the pointers...
initial vector is 5

N > P
-----
4 > 4
7
2 > 1
5 > 2
1 > 3

The initial vector points to 5th in the list because 1 is the lowest. 1 Points to 3rd in list because 2 is the next highest.2 points to 1 because 4 is the next highest etc...

The advantages of doing it this way is that you just sort the pointers, not the actual list.


Jesse(Posted 2005) [#7]
Yes, I understand that well. I knew about that, but my lazy vocabulary failed to properly express my intention... I finally learned how to do both in B+. Thanks all of you who showed interest in my request.