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
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