Hi all...
You know that im not very used to EB but i explore how works.
I've just made one small example with two linked list.
First list use strings and second integers.
I do this becose i think that can be useful to me in ABasic for storing variables with
his values in linked list.
I think that is not bad idea,and linked list are dinamical (something like ReDim).
Exemple is on very beginer level... ::)
'create two Linked List
'firs list is stringList,second is intList
varNameList = ListCreate()
varValueList = ListCreate()
'add varnames in List-----------------------------------
'1
varName = ListAdd(varNameList,NEW(STRING,1))
#<STRING>varName = "a"
varValue = ListAdd(varValueList,NEW(INT,1))
#<INT>varValue = 10
'2
varName = ListAdd(varNameList,NEW(STRING,1))
#<STRING>varName = "b"
varValue = ListAdd(varValueList,NEW(INT,1))
#<INT>varValue = 15
'3
varName = ListAdd(varNameList,NEW(STRING,1))
#<STRING>varName = "c"
varValue = ListAdd(varValueList,NEW(INT,1))
#<INT>varValue = 20
'4
varName = ListAdd(varNameList,NEW(STRING,1))
#<STRING>varName = "d"
varValue = ListAdd(varValueList,NEW(INT,1))
#<INT>varValue = 25
'-----------------------------------------------------
'search trough two List & find varnames and values
pos = ListGetFirst(varNameList)
pos2 = ListGetFirst(varValueList)
WHILE (pos <> 0) & (pos2 <> 0)
pName = ListGetData(pos)
pValue = ListGetData(pos2)
PRINT #<STRING>pName,"=",#<INT>pValue
pos = ListGetNext(pos)
pos2 = ListGetNext(pos2)
ENDWHILE
PRINT
PRINT "Press any key to close"
DO:until inkey$ <> ""
Zlatko
Do don't need 2 list.
type mytype
string name
int value
endtype
myList=ListCreate()
pointer var
settype var,mytype
var=ListAdd(myList,NEW(mytype,1))
#var.name="a"
#var.value=10
var=ListAdd(myList,NEW(mytype,1))
#var.name="b"
#var.value=20
var=ListAdd(myList,NEW(mytype,1))
#var.name="c"
#var.value=30
var=ListAdd(myList,NEW(mytype,1))
#var.name="d"
#var.value=40
pointer pos = ListGetFirst(myList)
WHILE (pos <> 0)
var = ListGetData(pos)
PRINT #var.name,"=",#var.value
pos = ListGetNext(pos)
ENDWHILE
Print
PRINT "Press any key to close"
waitcon
end
LarryMc
Thanks Larry...
I forget about UDT and Settype posibility, maby bacose i dont use this things to much.
Yes this way is better. :)
PS- Uops just small changes
not:
pointer pos = ListGetFirst(myList)
then:
pos = ListGetFirst(myList)
;D
Hi ..
I think that maby with Linked list i can build arrays in ABasic .
What you mean about that?
big difference Aurel
With arrays you can go to specific value if you know index.
With linked list you have to go through list to find one you want.
LarryMc
Maby you dont understand me Larry.
I mean for ABasic when user create array like
DIM a[5] that then create List with 5 elements.
I see something similiar in DLib source code that author create arrays with linked list.
Is this posibile?
Ups he dont do this with Linked list he realocate memory for arrays.
So this option probably wont work. ::)
Or you can suggest me what you mean what would be better or simplier solution?
Aurel
You would need to make your UDT something like:
TYPE array
int ndx
int type
UNION
uint uvalue
float fvalue
string svalue
ENDUNION
ENDTYPE
ndx would be to hold the array index number so you can find it in the linklist
type would tell you the type of variable
depending on type you would store the value in the appropriate union variable.
I don't know if this is a practical way for you to do this.
It might just be easier to have your array dim statement just point to a block of memory.
Then if you redimension have the REDIM statement create a new array and copy the old array to new array and then change the pointer to the location of the new array and delete the memory at the old array location.
LarryMc
Aurel
You probably would be better off in the long run if you stuck with what you discussed here:
http://www.ionicwind.com/forums/index.php/topic,3838.msg30333.html#msg30333
LarryMc
Ok ,Larry-no problem. ;)
Thanks on suggestion.
I will try first something with List then if not work as I espected i will try with memory.
Aurel,
You might want to look at the LinkList Class I made.
It has some extra functionality that EBasic's Linklist doesn't have built in.
You can download from here:
http://www.ionicwind.com/forums/index.php/topic,2613.msg22185.html#msg22185
Includes a help file with examples.
LarryMc
Oh i dont know that you make your own class.
Larry this looks great. ;)
i will study this...thanks!
Here is my another beginer step with LList.
'LinkedList test2
Type mytype
STRING name
INT value
Endtype
STRING GW1,GW2,GW3,GW4,GW5,GW6,GW7,GW8
INT WC,Pos,SPos,Epos
'--------------
String expr
Window win
'--------------
OPENWINDOW win,100,100,400,300,@minbox,0,"LList2",&main
SETWINDOWCOLOR win,rgb(220,220,220)
CONTROL win,@RICHEDIT,"",35,27,230,25,0x50800080,1
showwindow win,@SWHIDE,1:showwindow win,@SWRESTORE,1
CONTROL win,@SYSBUTTON,"GO",55,80,70,20,0x50000000,2
myList=ListCreate()
Pointer var
settype var,mytype
var=ListAdd(myList,NEW(mytype,1))
#var.name="a"
#var.value=10
temp = #var.value
var=ListAdd(myList,NEW(mytype,1))
#var.name="b"
#var.value=20
temp=temp + #var.value
posin = ListGetFirst(myList)
WHILE (posin <> 0)
var = ListGetData(posin)
'SETCONTROLTEXT win,1,#var.name +"="+ str$(#var.value)
posin = ListGetNext(posin)
ENDWHILE
SETCONTROLTEXT win,1,str$(temp)
'small text------------------------------
Setfont win,"verdana",10,400,0
Move win,10,110:Print win,"write:"
Move win,10,130:Print win,"a = 6"
Move win,10,150:Print win,"click GO"
Move win,10,170:Print win,"clear control"
Move win,10,190:Print win,"then write:"
Move win,10,210:Print win,"a = 3 + 1"
Move win,10,230:Print win,"click GO"
WAITUNTIL win=0
END
Sub main
SELECT @MESSAGE
CASE @IDCLOSEWINDOW
Closewindow win
CASE @IDCONTROL
IF @NOTIFYCODE=0
IF @CONTROLID=2
parse_expr()
calc_expr()
SETCONTROLTEXT win,1,str$(temp)
ENDIF
ENDIF
ENDSELECT
RETURN
ENDSUB
SUB parse_expr
expr=Getcontroltext(win,1)
IF expr=""
MESSAGEBOX 0,"It's empty,Enter Expression!","Ooups!"
Return
ENDIF
WC = 1
Pos = InStr(expr, " ",0)
While Pos > 0
WC = WC + 1
Pos = InStr(expr, " ",pos+1)
EndWhile
CountWords = WC
IF wc>0
SPos = 1
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW1 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>1
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW2 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>2
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW3 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>3
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW4 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>4
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW5 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>5
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW6 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>6
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW7 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>7
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW8 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
RETURN
ENDSUB
SUB calc_expr
'a = 5 + 1
IF GW2="=" & GW4="" & GW5="" :'set var value
var=ListAdd(myList,NEW(mytype,1))
#var.name=GW1
#var.value=val(GW3)
temp = #var.value
'Return 0
ENDIF
IF GW2="=" & GW4="+" & GW5<>"" :' calc expr after "="
PosInList = ListGetFirst(myList)
WHILE (PosInList <> 0)
var = ListGetData(PosInList)
IF #var.name=GW1 Then temp = #var.value + (val(GW3)+val(GW5))
PosInList = ListGetNext(PosInList)
ENDWHILE
'RETURN temp
ENDIF
RETURN temp
ENDSUB
Here is next step in my study of linked list.
I hope that you find it useful.
So in upper text box add something like this : c = 6 then press AddToList
then in down text box write variable name c then press FindInList...
'LinkedList test3
Type mytype
STRING name
INT value
Endtype
STRING GW1,GW2,GW3,GW4,GW5,GW6,GW7,GW8
INT WC,Pos,SPos,Epos
'--------------
String expr,temp$
INT ipos,error
Window win
'--------------------------------------------------------
OPENWINDOW win,100,100,610,400,@minbox,0,"LList3",&main
SETWINDOWCOLOR win,rgb(220,220,220)
'---------------------------------------------------------
CONTROL win,@RICHEDIT,"",20,27,200,25,0x50800080,1
showwindow win,@SWHIDE,1:showwindow win,@SWRESTORE,1
CONTROL win,@SYSBUTTON,"Add To List",250,27,100,20,0x50000000,2
CONTROL win,@RICHEDIT,"",20,320,200,25,0x50800080,3
showwindow win,@SWHIDE,3:showwindow win,@SWRESTORE,3
CONTROL win,@LISTBOX,"",19,72,164,244,0x50800140,4
CONTROL win,@LISTBOX,"",208,72,185,242,0x50800140,5
CONTROL win,@LISTBOX,"",408,72,185,242,0x50800140,6
CONTROL win,@SYSBUTTON,"Find In List",250,320,100,20,0x50000000,8
myList=ListCreate()
Pointer var
settype var,mytype
var=ListAdd(myList,NEW(mytype,1))
#var.name="a"
#var.value=10
temp = #var.value
var=ListAdd(myList,NEW(mytype,1))
#var.name="b"
#var.value=20
temp=temp + #var.value
posin = ListGetFirst(myList)
WHILE (posin <> 0)
var = ListGetData(posin)
'SETCONTROLTEXT win,1,#var.name +"="+ str$(#var.value)
posin = ListGetNext(posin)
ENDWHILE
SETCONTROLTEXT win,1,str$(temp)
'small text------------------------------
Setfont win,"Verdana",8,400,0
Move win,20,6:Print win,"Add here as x = 4"
WAITUNTIL win=0
END
Sub main
SELECT @MESSAGE
CASE @IDCLOSEWINDOW
Closewindow win
CASE @IDCONTROL
IF @NOTIFYCODE=0
IF @CONTROLID=2
parse_expr()
add_var()
'add var name in first listbox
ADDSTRING win,4,GW1
ENDIF
ENDIF
IF @NOTIFYCODE=0
IF @CONTROLID=8
find_var()
IF error=1
SETCONTROLTEXT win,3,"Variable not found!"
error=0
ENDIF
ENDIF
ENDIF
ENDSELECT
RETURN
ENDSUB
SUB parse_expr
expr=Getcontroltext(win,1)
IF expr=""
MESSAGEBOX 0,"It's empty,Enter Expression!","Ooups!"
Return
ENDIF
WC = 1
Pos = InStr(expr, " ",0)
While Pos > 0
WC = WC + 1
Pos = InStr(expr, " ",pos+1)
EndWhile
CountWords = WC
IF wc>0
SPos = 1
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW1 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>1
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW2 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>2
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW3 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>3
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW4 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>4
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW5 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>5
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW6 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>6
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW7 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
IF wc>7
SPos=EPos+2
EPos = InStr(expr, " ",SPos) - 1
If EPos <= 0 Then EPos = Len(expr)
GW8 = RTrim$(LTrim$(Mid$(expr, SPos, EPos - SPos + 1)))
ENDIF
RETURN
ENDSUB
'------------------------------------------------------------
SUB add_var
'a = 5 + 1
IF GW2="=" & GW4="" & GW5="" :'set var value
var=ListAdd(myList,NEW(mytype,1))
#var.name=GW1
#var.value=val(GW3)
temp = #var.value
'Return 0
ENDIF
RETURN
ENDSUB
'----------------------------------------------------------
SUB find_var
IF Getcontroltext(win,3)<>""
temp$=Getcontroltext(win,3)
temp$=Ltrim$(Rtrim$(temp$))
error=1
MESSAGEBOX 0,"TEMPSTR:"+temp$,"Ooups!"
PosInList = ListGetFirst(myList)
WHILE (PosInList <> 0)
var = ListGetData(PosInList)
IF #var.name=temp$
temp = #var.value
ADDSTRING win,5,#var.name : ADDSTRING win,6,str$(temp):error=0
ENDIF
PosInList = ListGetNext(PosInList)
ENDWHILE
ENDIF
IF temp$=""
SETCONTROLTEXT win,3,"No entry!"
ENDIF
RETURN error
ENDSUB