Page 1 of 3 123 LastLast
Results 1 to 10 of 21

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
    50
    Posts
    8,131
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  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
    50
    Posts
    8,131
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #5
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Leverkusen, Germany
    Age
    51
    Posts
    3,267
    Blog Entries
    2
    Rep Power
    338
    Eros, interesting thought. How would effect performance?3
    Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200.

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,131
    Blog Entries
    2
    Rep Power
    10
    Ciao Michael,

    what are you referring to regarding performances?

    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  7. #7
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Leverkusen, Germany
    Age
    51
    Posts
    3,267
    Blog Entries
    2
    Rep Power
    338
    I was wondering if implementing LLs into the core would have an impact on the parsing performance. Would it be slower?
    Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200.

  8. #8
    Junior Member Kuron's Avatar
    Join Date
    Sep 2017
    Location
    Nashville
    Posts
    8
    Blog Entries
    1
    Rep Power
    1
    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
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Leverkusen, Germany
    Age
    51
    Posts
    3,267
    Blog Entries
    2
    Rep Power
    338
    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.
    Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200.

  10. #10
    Junior Member Kuron's Avatar
    Join Date
    Sep 2017
    Location
    Nashville
    Posts
    8
    Blog Entries
    1
    Rep Power
    1
    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 3 123 LastLast

Similar Threads

  1. 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
  2. Linked List Tool / Helper
    By kryton9 in forum User tools
    Replies: 5
    Last Post: 25-05-2008, 13:39
  3. dir_listarray and linked lists
    By kryton9 in forum thinBasic General
    Replies: 2
    Last Post: 14-05-2007, 07:25
  4. Sorting arrays with built-in sorting functions
    By ErosOlmi in forum Execution speed tests
    Replies: 4
    Last Post: 18-03-2007, 23:05
  5. Linked Lists
    By Petr Schreiber in forum thinBasic General
    Replies: 1
    Last Post: 22-09-2005, 23:10

Posting Permissions

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