PDA

View Full Version : sorting the Linked List



primo
20-08-2017, 16:55
i want to sort the Linked Lists (LL), but it cause a "blurring" in my brain !!
i suppose this situation:
B -> 100
C -> 50
A -> 200
D -> 10

and by sorting it (Descending) we should get
A -> 200
B -> 100
C -> 50
D -> 10

my approach is to get all the numbers in an array and to sort it with array sort, after that i go through the array one by one and then searching the numbers in the LL , i then update the linked list data to equal the array index (i).

better an example
the console output in the last 4 lines is:
2 b
3 c
1 a
4 d
this info say that A have a highest score 200 -> 1.
B have the second score
C the third
D the fourth

not so bad from the practical view point

uses "LL", "Console"

function TBMain()

Local llRoot, llItem, ptItem As DWord
local Counter as long

Dim arr(4) As Long

' -- We create linked list root here, basically a first node
llRoot = LL_Add(0, "B", 100)

' -- We add item "A" after root node
llItem = LL_Add(llRoot, "C", 50)

' -- We add item "B"
LL_Add(llRoot, "A", 200)

' -- We add item "C"
LL_Add(llRoot, "D", 10)

' -- Now we can list data of all nodes
Dim i As Long

For i = 1 To LL_Count(llRoot)
PrintL LL_Data( LL_GetByNumber(llRoot, i) )
Next
PrintL
For i = 1 To LL_Count(llRoot)
llItem = LL_GetByNumber(llRoot, i)
arr(i) = LL_Data(llItem)
Next

Array Sort arr(), Collate Ucase, Descending
For i = 1 To 4
PrintL arr(i)
Next
PrintL
For i = 1 To 4
ptItem = LL_FindByData(llRoot, arr(i))
LL_Update(ptItem, i)
Next

For i = 1 To 4
ptItem = LL_GetByNumber(llRoot, i)
PrintL LL_Data( ptItem ) & " " & LL_Name(ptItem)
Next

' -- Now we free all data
ll_Free(llRoot)

waitkey

end function

ErosOlmi
21-08-2017, 10:13
Dear primo,

LL module is a very old module and I think I will remove sooner or later from thinBasic distribution.

Have a look at Core Data Structures: http://www.thinbasic.com/community/showthread.php?12737-some-arrays-goodies&p=93356#post93356
In particular AVL Tree that is always internally sorted by the key: http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?tree_avl.htm
Examples in \thinBasic\SampleScripts\DataStructures\

Ciao
Eros

primo
21-08-2017, 12:23
Thanks Eros for the suggestions
in fact i suggest to keep the LL module, because it is one of the computer science subjects, it is fun to use and easy to understand its functions. my above post is not a critic but a toying with the module for the first time. the module contains most needed functions which is easy to use. look as an example http://www.thinbasic.com/community/showthread.php?8930-Linked-List-Tool-Helper&highlight
certainly i will look at the other tools.
regards

ErosOlmi
21-08-2017, 20:12
Yes, sorry. And thanks for you nice example.

My reply is a "critic" to myself grrr :oops:
It is a lot of time I wanted to implement a better LL set of functions directly into Core engine but never had time, ... Also wanted to add a native stack and a queue data structure.
I will see what I can do in next releases.

Michael Hartlef
30-08-2017, 08:42
Eros, interesting thought. How would effect performance?3

ErosOlmi
30-08-2017, 17:03
Ciao Michael,

what are you referring to regarding performances?

Eros

Michael Hartlef
31-08-2017, 06:07
I was wondering if implementing LLs into the core would have an impact on the parsing performance. Would it be slower?

Kuron
20-09-2017, 07:37
LL module is a very old module and I think I will remove sooner or later from thinBasic distribution.


Please do not remove this. It is extremely useful and nothing is gained by needlessly making things overly complex.

Michael Hartlef
21-09-2017, 10:09
LinkedLists, Maps, Stacks are super useful containers and I consider them a must for any good language out there. Same goes for native Json and XML functionality.

Kuron
21-09-2017, 12:05
LinkedLists, Maps, Stacks are super useful containers and I consider them a must for any good language out there. Same goes for native Json and XML functionality.

LinkedLists and XML, I understand. Stacks I am familiar with from years of studying Compiler/VM design. Maps I do not really understand and I am completely clueless about Json. I do need to try and learn about the last two (especially Json), but since my TBI in 2014, I have a very difficult time trying to learn new things.

Anyway, new to TB and decided to check it out since FBSL has disappeared and I no longer have access to it. Not a lot of choices left for indie programming languages.

Kuron
21-09-2017, 22:05
@Primo -- Thank you VERY much for those working links. I did try archive.org first, but it is so difficult to find a capture that actually has working links. You encounter enough links that do not work, and you give up. Now, if I can find a working link to the FBSL v3.5 RC2.zip file, I will be back in shape and where I need to be.

thinBasic is impressive. I do find the forums difficult to navigate (way too many sections which makes it cluttered IMHO), but I am impressed with TB, and this is from somebody who is not a huge fan of interpreted languages. I have been trying all of the examples. Feature-wise, thinBasic is what PB should have been. I am struggling with TB a good bit, but that is 100% on me not knowing/understanding what I am doing. :D

Michael Hartlef
22-09-2017, 13:50
Kuron = Brice Manuel ?

Kuron
22-09-2017, 14:32
Kuron = Brice Manuel ?

'Tis no secret... Your point?

Michael Hartlef
22-09-2017, 17:47
'Tis no secret... Your point?

Thanks, no point, just confirmation.

Kuron
22-09-2017, 18:34
Thanks, no point, just confirmation.

NP. ;) FWIW, Kuron was the name of my D&D character in late 70s, and when I jumped online in 1980, I have used Kuron anywhere I could and since the mid 80s, I have used Kuron as my name in music, radio and TV. Pretty much since the 80s, everybody (including family) has called me Kuron. One of these days within the next few years, I plan to get my name legally changed to Kuron.

Kuron
22-09-2017, 18:43
@primo Thank you for all of your help, unfortunately Mediafire has blocked that file, so it can't be downloaded.

Kuron
22-09-2017, 19:23
I grabbed it. Thank you very much for all of your help, Primo. I appreciate it more than you could ever imagine.

Kuron
23-09-2017, 17:18
Thank you, Eros and primo.

ErosOlmi
25-09-2017, 20:41
Dear all,

I'm un-approving some of recent posts in order to see what posts are causing Google to consider our forum an insicure place.
Some are also my own posts where inside there are external links.

If I will be able to understand the reason why Google is doing that, I will re-approve posts.

Thanks
Eros