Help Learning Linked Lists

Kenny

Back for now.
Reaction score
202
So a while back, we started to see a lot of "black magic" pop up (I'm looking at you Jesus4Lyf :p), and i started wondering if it would be worth learning linked lists, as I am a ridiculously slow learning when it comes to new concepts.

Then after making a few spells and stuff, I realised that I am using the struct stacks a lot, and maybe it would be helpful to implement a linked list that can be used to keep track of the currently used structs to reduce complexity a little, and by complexity I mean making it easier to write and stuff. I'm not sure whether its faster or not, but I would assume they would be around the same.

So I went a-learnin' and basically I've come up with this:

JASS:
library LLM

    //! textmacro LIST
    
        integer next  = 0
        integer prev  = 0

        static integer first = 0
        static integer last  = 0
        static integer max   = 0
        
        method linkNode takes nothing returns boolean
            if .first == 0 then
                set .first = this
            else
                set .prev = .last
                set .prev.next = this
            endif
            
            set .last = this
            set .max = .max + 1
                
            return true
        endmethod
        
        method unlinkNode takes nothing returns boolean
            if .first == this then
                set .first = .next
            endif
            
            if .last == this then
                set .last = .prev
            endif
            
            set .prev.next = .next
            set .next.prev = .prev
            set .max = .max - 1
            
            return true
        endmethod
        
        method getNext takes nothing returns integer
            return .next
        endmethod
        
        method getPrev takes nothing returns integer
            return .prev
        endmethod
        
        method getMax takes nothing returns integer
            return .max
        endmethod
        
    //! endtextmacro
    
endlibrary


Okay so I'm not even sure if it works as I'm not on my home computer, so feedback is good. If it doesn't work I don't want it done for me (at first :p), maybe just some hints.

What I want to do in the future:

- Add a boolean variable to see whether or not a struct is already assigned to a node.
- Anything that anyone can point out.

Thanks in advance.

EDIT:

Oh and the reason I am using textmacro and integer members is because I don't have the latest Jasshelper, I want to be able to test it when i get on my home computer. I will probably update it when I read the jasshelper manual a bit more.
 

Kenny

Back for now.
Reaction score
202
Maybe you could take a look at this for some reference.


Thanks, but for now, I'd rather not look at any other peoples systems involving linked lists, as I think I absorb what needs to be learnt better that way.

Anywho, I read through the JassHelper manual and decided to make it use thistype as it would be so much better. This is what I have:

JASS:
library List

    module List
    
        private thistype next = 0
        private thistype prev = 0
        private boolean  link = false

        private static thistype first = 0
        private static thistype last  = 0
        private static thistype size  = 0
        
        method insertNode takes nothing returns boolean
            if .link == false then
                set .link = true
                
                if .first == 0 then
                    set .first = this
                else
                    set .prev = .last
                    set .prev.next = this
                endif
                
                set .last = this
                set .size = .size + 1
                
                return true
            endif
                
            return false
        endmethod
        
        method removeNode takes nothing returns boolean
            if .link == true then
                set .link = false
                
                if .first == this then
                    set .first = .next
                endif
                
                if .last == this then
                    set .last = .prev
                endif
                
                set .prev.next = .next
                set .next.prev = .prev
                set .size = .size - 1
                
                return true
            endif
            
            return false
        endmethod
        
        //! textmacro Get takes STATIC, NAME, TYPE, VALUE
        $STATIC$ method $NAME$ takes nothing returns $TYPE$
            return .$VALUE$
        endmethod
        //! endtextmacro

        //! runtextmacro Get("","getNext","thistype","next")
        //! runtextmacro Get("","getPrev","thistype","prev")
        //! runtextmacro Get("","isLink","boolean","link")
        //! runtextmacro Get(" static","getFirst","thistype","first")
        //! runtextmacro Get(" static","getLast","thistype","last")
        //! runtextmacro Get(" static","getSize","thistype","size")
        
    endmodule

endlibrary


Questions:

- Do I need to be able to release the list? If so, does that mean I need to unlink each node before i destroy them?
- Do my methods for getFirst, getLast and getSize need to be static? (I changed the textmacro to accompany all methods).
- Kind of ties in with point one. I was thinking about adding flushList and destroyList functionality. Is this possible? (This is where I'm stumped i think).
- Anything else I should add?
 

Tom Jones

N/A
Reaction score
437
Nope.

Nope.
JASS:

struct ...
      list l

      method ...
          local list l = .l.first

          loop
              exitwhen l == 0
              ...
              set l = l.next
          endloop
      endmethod
endstruct


A) Unlink all listmembers. B) Add a onDestroy method.
 

Kenny

Back for now.
Reaction score
202
Sorry to disappoint, but:

I am a ridiculously slow learning when it comes to new concepts

:p

Anywho, I get most of what your saying, just still a little confused.

I think I'm going to keep my various method textmacro-majig as is until someone shows me why it shouldn't be like that.

I am trying to figure out a way to save a flush method and a destroy method. What happens if for some reason, a node gets added while im flushing or destroying, what can i do to counter it?

Also by onDestroy method, did you mean a destroyList method or just the plain old onDestroy? If so, what would be its purpose?

EDIT:

Oh and I was finally able to test it and it worked. Hell yeah, first attempt at something to do with linked lists and it worked0. Even though it is pretty basic.


EDIT 2:

Been working on it a bit more. Only really found flushing the list necessary (As opposed to removing and destroying nodes). Someone is welcome to tell me otherwise. Here's what I have now:

JASS:
library Linked

    public module List
    
        static timer Timer = null           // Timer for personal use.
        
        private thistype next = 0           // Next node link.
        private thistype prev = 0           // Prev node link.
        private boolean  link = false       // If link is added to list.

        private static thistype head = 0    // Head == first node in list.
        private static thistype last = 0    // Last == last node added to list.
        private static integer  size = 0    // Size == How many nodes are currently in the list.
        
        // Insert a node into the list:
        method insertNode takes nothing returns boolean
            if .link == false then
                set .link = true
                
                if .head == 0 then
                    set .head = this
                else
                    set .prev = .last
                    set .prev.next = this
                endif
                
                set .last = this
                set .size = .size + 1
                
                return true
            debug else
                debug call BJDebugMsg("Error: Attempt to insert previously existing node into list.")
            endif
                
            return false
        endmethod
        
        // Remove a node from the list:
        method removeNode takes nothing returns boolean
            if .link == true then
                set .link = false
                
                if .head == this then
                    set .head = .next
                endif
                
                if .last == this then
                    set .last = .prev
                endif
                
                set .prev.next = .next
                set .next.prev = .prev
                set .size = .size - 1
                
                return true
            debug else
                debug call BJDebugMsg("Error: Attempt to remove currently inexistant node from list.")
            endif
            
            return false
        endmethod
        
        // BJ function. Two for one:
        method deleteNode takes nothing returns nothing
            if .removeNode() then
                call .destroy()
            endif
        endmethod
        
        // Flush all nodes in the list:
        static method flushList takes nothing returns nothing
            local thistype d = .head
            
            loop
                exitwhen d == 0
                call d.removeNode()
                set d = d.next
            endloop
        endmethod
        
        // For personal timer:
        static method listInit takes nothing returns nothing
            set .Timer = CreateTimer()
        endmethod
        
        // Creates all the small useful functions needed:
        //! textmacro GetFunctions takes STATIC, NAME, TYPE, VALUE
        $STATIC$ method $NAME$ takes nothing returns $TYPE$
            return .$VALUE$
        endmethod
        //! endtextmacro

        //! runtextmacro GetFunctions("","getNext","thistype","next")
        //! runtextmacro GetFunctions("","getPrev","thistype","prev")
        //! runtextmacro GetFunctions("","isLink","boolean","link")
        //! runtextmacro GetFunctions(" static","getFirst","thistype","head")
        //! runtextmacro GetFunctions(" static","getLast","thistype","last")
        //! runtextmacro GetFunctions(" static","getSize","thistype","size")
        
    endmodule

endlibrary


Everything seems to work okay. Just wondering what input some more experience members might have.

The timer in there is just because I found myself needing a timer whenever I needed this module, and it made it easier. I know it doesn't really belong, but oh well.

The question still remains: What if I somehow add a node to the list, while it is currently flushing or something? Should I find a way to prevent this? I'm currently trying to think of a way, but nothing significant has popped into my head.
 

Kenny

Back for now.
Reaction score
202
Bump. Last question in my last post still remains. And is everything else okay with it? Or have I missed something important?
 

Builder Bob

Live free or don't
Reaction score
249
As there's nothing in your flushing method that can run any other code throughout the process, it will be impossible to insert a node while it's flushing. As soon as the flushing starts, it will finish before any other code can run.

Or am I missing anything?
 

Kenny

Back for now.
Reaction score
202
Ohh fair enough then, i just thought there may be a situation where they can both somehow be run simultaniously, or insertNode() to be used just when flushList() is ending, so a new node gets flushed instantly.

But thats unlikely, lol.
 

Builder Bob

Live free or don't
Reaction score
249
Well, war3 cannot run multiple threads of code simultaneously.

When an event triggers or a timer expires, they will actually wait for any and all other already running code to end before running.
 

Kenny

Back for now.
Reaction score
202
Aiight, cool. Thanks for the clarification. I thought of a way to fix it using a boolean, but i guess its not necessary anymore.

Can you see any problems with the rest of the script?

Everything seems to work okay on my end, and it shaved off lke 32 lines from one of my spells, and makes it so much easier to code, I was surprised by how useful it is.
 

Builder Bob

Live free or don't
Reaction score
249
Can you see any problems with the rest of the script?

Everything seems to work okay on my end, and it shaved off lke 32 lines from one of my spells, and makes it so much easier to code, I was surprised by how useful it is.

Looks good to me. Linked lists are awesome ^^
 

Kenny

Back for now.
Reaction score
202
Looks good to me. Linked lists are awesome ^^

Thanks for your help, and yes they are awesome. It saves quite a bit of time.

Anyway, for anyone who reads this. Do you think its worth submitting?

I just thought, with the trend of people using struct array loops, this could come in very handy. And I just thought I'd ask now to save it being a waste of a thread.

Final Product (Hopefully):

JASS:
library Linked

    public module List
    
        static timer Timer = null           // Timer for personal use.
        
        private thistype next = 0           // Next node link.
        private thistype prev = 0           // Prev node link.
        private boolean  link = false       // If link is added to list.

        private static thistype head = 0    // Head == first node in list.
        private static thistype last = 0    // Last == last node added to list.
        private static integer  size = 0    // Size == How many nodes are currently in the list.
        
        // Insert a node into the list:
        method insertNode takes nothing returns boolean
            if .link == false then
                set .link = true
                
                if .head == 0 then
                    set .head = this
                else
                    set .prev = .last
                    set .prev.next = this
                endif
                
                set .last = this
                set .size = .size + 1
                
                return true
            debug else
                debug call BJDebugMsg("Linked List (insertNode error): Attempt to insert previously existing node into list.")
            endif
                
            return false
        endmethod
        
        // Remove a node from the list:
        method removeNode takes nothing returns boolean
            if .link == true then
                set .link = false
                
                if .head == this then
                    set .head = .next
                endif
                
                if .last == this then
                    set .last = .prev
                endif
                
                set .prev.next = .next
                set .next.prev = .prev
                set .size = .size - 1
                
                return true
            debug else
                debug call BJDebugMsg("Linked List (removeNode error): Attempt to remove currently inexistant node from list.")
            endif
            
            return false
        endmethod
        
        // BJ function. Two for one:
        method deleteNode takes nothing returns nothing
            if .removeNode() then
                call .destroy()
            debug else
                debug call BJDebugMsg("Linked List (deleteNode error): Unabled to destroy data attached to node.")
            endif
        endmethod
        
        // Flush all nodes in the list:
        static method flushList takes boolean wantDelete returns nothing
            local thistype d = .head
            
            loop
                exitwhen d == 0
                call d.removeNode()
                if wantDelete then
                    call d.destroy()
                endif
                set d = d.next
            endloop
        endmethod
        
        // For personal timer:
        static method createTimer takes nothing returns nothing
            set .Timer = CreateTimer()
        endmethod
        
        // Creates all the small useful functions needed:
        //! textmacro GetFunctions takes STATIC, NAME, TYPE, VALUE
        $STATIC$ method $NAME$ takes nothing returns $TYPE$
            return .$VALUE$
        endmethod
        //! endtextmacro

        //! runtextmacro GetFunctions("","getNext","thistype","next")
        //! runtextmacro GetFunctions("","getPrev","thistype","prev")
        //! runtextmacro GetFunctions("","isLink","boolean","link")
        //! runtextmacro GetFunctions(" static","getFirst","thistype","head")
        //! runtextmacro GetFunctions(" static","getLast","thistype","last")
        //! runtextmacro GetFunctions(" static","getSize","thistype","size")
        
    endmodule

endlibrary
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top