[Snippet] LinkedListModule

Level 6
Joined
Jun 20, 2011
Messages
249
Yes of course, v2.1.0 did worked, make sure your struct doesn't have an allocation method already, this resource has it's own, also i just updated it to 2.1.1 the only change is that the head, next and prev variables are now public
 
Level 6
Joined
Jun 20, 2011
Messages
249
Updated to v2.1.2
-The recycle stack now uses prev instead of next as the pointer because this way the user is allow to know which is the node's "next" pointer even after removed and deallocated, and since "next" is the most common way to iterate through a linked list this is very useful. DISCLAIMER: if you pretend to iterate using "prev" remember to use a variable to store the node's previous "prev" pointer before removing and deallocating it.
 
Level 29
Joined
Mar 10, 2009
Messages
5,016
Funny, I cant implement the module coz it says;
Member redeclared: allocate

But when I change this...
JASS:
static method allocate takes nothing returns thistype

method deallocate takes nothing returns nothing

static method createNode takes nothing returns thistype
   local thistype this=allocate()

to this...
JASS:
static method alloc takes nothing returns thistype

method dealloc takes nothing returns nothing

static method createNode takes nothing returns thistype
   local thistype this=alloc()

//or this

static method pizza takes nothing returns thistype

method eatme takes nothing returns nothing

static method createNode takes nothing returns thistype
   local thistype this=pizza()

suddenly it worked...
 
Level 17
Joined
Apr 27, 2008
Messages
2,453
Any reason why you use this :

JASS:
        method insertNode takes thistype toInsert returns nothing            
            set this.next.prev=toInsert
            set toInsert.next=this.next
            set this.next=toInsert
            set toInsert.prev=this
        endmethod

Instead of this :

JASS:
        method insertNode takes thistype toInsert returns nothing            
            set this.prev.next=toInsert
            set toInsert.prev=this.prev
            set this.prev=toInsert
            set toInsert.next=this
        endmethod

I suppose you have, i don't have really thought about that, i'm just curious.
 
Level 6
Joined
Jun 20, 2011
Messages
249
Queues work like that.
I did include the other method but people told me that it was consuming lots of code so i deleted it, anyways doing this would have the exact same result as that
this.prev.insertNode(toInsert)
If you want to add the node to the "end' of the list
 
Level 6
Joined
Jun 20, 2011
Messages
249
feel free to add these lines to the module
JASS:
//This method joins two lists or two heads together without breaking it (do not merge loose nodes)
static method mergeNode takes thistype toMergeWith returns nothing
    set this.next.prev = toMergeWith.prev
    set toMergeWith.prev.next = this.next
    set this.next = toMergeWith
    set toMergeWith.prev = this
endmethod

method insertBefore takes thistype toInsert returns nothing
    set this.prev.next=toInsert
    set toInsert.prev=this.prev
    set this.prev=toInsert
    set toInsert.next=this
endmethod

/* optional insertBefore method
method insertBefore takes thistype toInsert returns nothing
    call this.prev.insertNode(toInsert)
endmethod
*/
 
Level 6
Joined
Jun 20, 2011
Messages
249
v2.2.0
-The insertNode method now inserts the node behind the given node (append)

This update "sorta" breaks backwards compatibility, make sure to do the proper fixes.
(Same reason why i'm updating MergeSort just now)
 
Level 6
Joined
Jun 20, 2011
Messages
249
v2.3.0
-Added the new LinkedListLite module to avoid many lines of code from being generated when implementing the module into a struct and not using the methods it provides, it's also slightly faster (but you have to use the textmacros)
-Added a list of textmacros useful to handle linked lists safely when using the LinkedListLite module.

Any idea for a new textmacro is welcome
 
Top