Page 1 of 1
Linkedlist help needed
Posted: Mon Aug 01, 2005 9:21 pm
by ravagepn
I want to make a simple lottery program with linkedlist but I'm not sure how to do it.
Here is my code:
---------------------------------------
NewList mylist.l()
For i = 1 To 39
AddElement(mylist())
mylist() = i
Next
PrintN("Result: "+Str(mylist()))
;And PureBasic crushes ...
---------------------------------------
Here is how I want my program to work:
I want to make list like: mylist.l(1,2,3 ... 38, 39)
and exitlist() - which should be empty at start
Then I want to randomly substract seven numbers, one by one, from mylist() and add them to exitlist()
At the end of program I want to print exitlist()
Can it be done this way and is it better that I use some other way ?
I did this program this way in python programming language but I don't know how to do it in pb
Posted: Mon Aug 01, 2005 10:57 pm
by fweil
ravagepn,
Should this be coded like that ?
Code: Select all
NewList mylist.l()
NewList exitlist.l()
For i = 1 To 39
AddElement(mylist())
mylist() = i
Next
For i = 1 To 7
index = Random(39)
AddElement(exitlist())
exitlist() = index
SelectElement(mylist(), index - 1)
DeleteElement(mylist())
Next
ResetList(exitlist())
While NextElement(exitlist())
Debug exitlist()
Wend
End
But in my opinion, at this level of features, I suspect there is no need of mylist !
Well, according to what you want to do then, I hope this helps.
Rgrds
Posted: Mon Aug 01, 2005 11:22 pm
by SoulReaper
Hello all
Thankyou fweil
I was thinking of using the Dim command as I thought that was the right way LOL - I guess its the best way that matters...
The Dimmed way was getting confusing LOL :roll:
Guess I still have a lot to learn...

Posted: Mon Aug 01, 2005 11:23 pm
by fweil
I ran too fast there !
Code: Select all
NewList mylist.l()
NewList exitlist.l()
NewList stats.l()
For i = 0 To 39
AddElement(stats())
stats() = 0
Next
Repeat
ClearList(mylist())
ClearList(exitlist())
For i = 0 To 39
AddElement(mylist())
mylist() = i
Next
nElements = 38
For i = 0 To 6
index = Random(38) + 1
SelectElement(mylist(), index)
value = mylist()
DeleteElement(mylist())
nElements - 1
AddElement(exitlist())
exitlist() = value
SelectElement(stats(), index)
stats() + 1
Next
ResetList(exitlist())
a$ = ""
While NextElement(exitlist())
a$ + Str(exitlist()) + " "
Wend
Debug a$
ResetList(stats())
a$ = ""
NextElement(stats())
While NextElement(stats())
a$ + Str(stats()) + " "
Wend
Debug a$
Until Quit
End
I added a stats() list to check the accuracy of random generator.
And mylist() is rather usefull in fact !
Rgrds
Posted: Tue Aug 02, 2005 2:20 pm
by ravagepn
Thanks, compiling is ok, but how do I display result (exitlist) and where should I put openconsole - closeconsole in your code ? Does debug command have to be used, I want to make simpliest program as possible at the beggining and later when I understand the code I'll add more
Posted: Tue Aug 02, 2005 2:30 pm
by fweil
Does this answer ? Here you have a single result printed on the console.
Anyway you can call the procedure as many times you need.
Code: Select all
NewList mylist.l()
NewList exitlist.l()
Procedure Pull(NItems, Winning.l)
ClearList(mylist())
ClearList(exitlist())
For i = 0 To NItems
AddElement(mylist())
mylist() = i
Next
nElements = NItems - 1
For i = 0 To Winning - 1
index = Random(NItems - 1) + 1
SelectElement(mylist(), index)
value = mylist()
DeleteElement(mylist())
nElements - 1
AddElement(exitlist())
exitlist() = value
Next
EndProcedure
;
;
;
OpenConsole()
Pull(39, 7)
ResetList(exitlist())
While NextElement(exitlist())
Print(Str(exitlist()) + " ")
Wend
PrintN("")
While Inkey() = ""
Wend
CloseConsole()
End
Rgrds
Posted: Tue Aug 02, 2005 11:02 pm
by ravagepn
Thank you !! I'm going now to do some code research

Posted: Wed Aug 03, 2005 1:19 pm
by ravagepn
I've looked at your code whole morning and started to understand it partially. One problem I can't solve out, how to sort linkedlist ?
Suppose I have a linkedlist named exitlist() which contains 7 random numbers from 1 to 39 I want to sort it in asccending order.
Here is what I thougt will work (but it doesn't):
Code: Select all
For i = 1 To 6
For j = i + 1 To 7
SelectElement(exitlist(), i)
*FirstElement = @exitlist()
SelectElement(exitlist(), j)
*SecondElement = @exitlist()
If *FirstElement > *SecondElement
SwapElements(exitlist(), *FirstElement, *SecondElement)
EndIf
Next
Next
Posted: Thu Aug 04, 2005 6:35 am
by fweil
As actually SortList() does not work you can use SortArray() after copying the list in an array :
Code: Select all
NewList MyList.l()
OpenConsole()
For i = 1 To 20 ; set list content and list it
AddElement(MyList())
MyList() = Random(50)
Print(Str(MyList()) + " ")
Next
PrintN("")
SortList(MyList(), 0) ; Does not work actually !
ResetList(MyList()) ; display the list content
While NextElement(MyList())
Print(Str(MyList()) + " ")
Wend
PrintN("")
NItems = CountList(MyList())
Dim MyArray.l(NItems - 1) ; Now use an array and copy the list content
ResetList(MyList())
i = 0
While NextElement(MyList())
MyArray(i) = MyList()
i + 1
Wend
SortArray(MyArray(), 0) ; Sort the copy and send sorted data in the cleared list
ClearList(MyList())
For i = 1 To NItems
AddElement(MyList())
MyList() = MyArray(i - 1)
Next
ResetList(MyList()) ; display the list content
While NextElement(MyList())
Print(Str(MyList()) + " ")
Wend
PrintN("")
While Inkey() = ""
Wend
CloseConsole()
End
Posted: Thu Aug 04, 2005 5:30 pm
by ravagepn
Thank you again. I figured that out but I was hoping that there is a simple way. I started to learn PureBasic few days ago and I'm a little disappointed that lists are so complicated. In Python for example whole this job can be done with few commands in 2-3 raws. I hope I'll find PB's good sides in other routines, as soon I continue learning.