Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: sorting the Linked List

  1. #1

    sorting the Linked List

    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
    

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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/s...3356#post93356
    In particular AVL Tree that is always internally sorted by the key: http://www.thinbasic.com/public/prod...l?tree_avl.htm
    Examples in \thinBasic\SampleScripts\DataStructures\

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    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/s...lper&highlight
    certainly i will look at the other tools.
    regards

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Yes, sorry. And thanks for you nice example.

    My reply is a "critic" to myself grrr
    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.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    Eros, interesting thought. How would effect performance?3

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Ciao Michael,

    what are you referring to regarding performances?

    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  7. #7
    I was wondering if implementing LLs into the core would have an impact on the parsing performance. Would it be slower?

  8. #8
    Member Kuron's Avatar
    Join Date
    Sep 2017
    Location
    Nashville
    Posts
    54
    Rep Power
    12
    Quote Originally Posted by ErosOlmi View Post
    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.

  9. #9
    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.

  10. #10
    Member Kuron's Avatar
    Join Date
    Sep 2017
    Location
    Nashville
    Posts
    54
    Rep Power
    12
    Quote Originally Posted by Michael Hartlef View Post
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Sorting arrays with built-in sorting functions
    By ErosOlmi in forum Execution speed tests
    Replies: 6
    Last Post: 21-02-2024, 21:36
  2. A problem conc. Dynamic compiling using linked functions from within O2
    By RobbeK in forum O2 JIT Compiler / Assembler / Oxygen project
    Replies: 10
    Last Post: 08-01-2014, 23:30
  3. Linked List Tool / Helper
    By kryton9 in forum User tools
    Replies: 5
    Last Post: 25-05-2008, 13:39
  4. dir_listarray and linked lists
    By kryton9 in forum thinBasic General
    Replies: 2
    Last Post: 14-05-2007, 07:25
  5. Linked Lists
    By Petr Schreiber in forum thinBasic General
    Replies: 1
    Last Post: 22-09-2005, 23:10

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •