- Joined
- Dec 12, 2008
- Messages
- 7,385
Linked Lists
This is an easy tutorial that explains the art of using a linked list.
What is a linked list?
A linked list is a data structure.
The name explains it all.
It links nodes in a list.
What I like about linked lists is that allocation/deallocation is childs-play
Where could this be useful?
It's commonly used to link struct instances so you can loop through them easily
What do I require?
For a linked list, you only require 2 global integer arrays ^^
JASS:
globals
integer array next
integer array prev
endglobals
In structs they would look like this:
JASS:
struct MyStruct
static integer array next
static integer array prev
endstruct
Is that it? -_-
No..
You still need to know the algorithms...
A linked list is literally a list.
To add data to a linked list, we have to go to the end of that list, and put the data there
JASS:
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
// Add 'this' to the list
set next[this] = 0
set prev[this] = prev[0]
set next[prev[this]] = this
set prev[0] = this
return this
endmethod
All these lines can be used to create pseudo code that makes a lot of sense
set next[this] = 0
In this type of linked list, the end is represented by a '0'
Here, we set the next node of this to 0.
set prev[this] = prev[0]
Here, we set the previous node of this to 'prev[0]' which we're using as a reference to the last added node since logically, the previous node of 0 is not needed and will never be used .. lol
set next[prev[this]] = this
This one is quite logical.
This is how you should read it:
We're setting the next one of the previous one to this one.
See how that works?
set prev[0] = this
We're using the 'null' pointer to store the current node because it would count as the last allocated node in the list next time we add a new node ^^
Well, now that you know how to add data to a linked list, I'll teach you how to remove data from a linked list.
JASS:
method destroy takes nothing returns nothing
set next[prev[this]] = next[this]
set prev[next[this]] = prev[this]
call .deallocate()
endmethod
Well, this can be confusing for some people, and that's why I drew this to help you
'picture' the deallocation algorithm at work.
See, we want to remove 'this' from the list, but how could we do that?
The algorithm is quite easy.
See, the next node of the previous node of this happens to be this,
and the previous node of the next node of this also happens to be this.
So, if we set the next node of the previous node of this to the next node of this
and if we set the previous node of the next node of this to the previous node of this,
'this' will be completely gone from the linked list
Pretty easy huh?
Yeah I know it's confusing, but you'll get used to it
Okay.. I have my linked list.. Now what am I supposed to do with it
Iteration.. DUH!
We use linked lists to loop through nodes.
Look at this linked list:
JASS:
struct List
static integer array next
static integer array prev
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set next[this] = 0
set prev[this] = prev[0]
set next[prev[this]] = this
set prev[0] = this
return this
endmethod
static method iterate takes nothing returns nothing
local integer index = next[0]
loop
exitwhen index==0
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,I2S(index))
set index = next[index]
endloop
endmethod
method destroy takes nothing returns nothing
set next[prev[this]] = next[this]
set prev[next[this]] = prev[this]
call .deallocate()
endmethod
endstruct
Our iteration algorithm is very pseudo-friendly too ^^
General Iteration:
1- Get the first node
2- If the node is null we stop
3- Go to the next node.
4- Go to number 2
Iteration here:
1- Get the first node.
local integer index = next[0]
next[0] is the first node in this linked list
2- If the node is null we stop
exitwhen index == 0
In our linked list, 0 represents a null node
3- Go to the next node
set index = next[index]
Pretty simple :P
4- Go to number 2
In the allocation method we set the next node of the last allocated node to 0
This will allow us to exit when the list ends :)
[b][size="2"]Misc Tips[/size][/b]
There are lots of cool things you could do with a linked list:
[icode=jass]set next[0] = 0
This makes iteration impossible
set prev[0] = 0
This forces the list to begin overwriting itself during further allocation
JASS:
set next[0] = 0
set prev[0] = 0
This resets the list ^^
Wrap-Up
As you can see, linked lists are easy to use, efficient, and very pseudo-friendly data structures
I recommend using them for anything that requires looping through struct instances ^^
Thank you for reading this tutorial.
Feel free to comment.
~Magtheridon96