¿Qué es esto?https://en.wikipedia.org/wiki/Doubly_linked_list
Codigo- Spoiler:
-
[jass]library DoublyLinkedList
module DoublyLinkedList
readonly thistype head
private thistype _first
method operator first takes nothing returns thistype
return this.head._first
endmethod
private thistype _last
method operator last takes nothing returns thistype
return this.head._last
endmethod
readonly thistype prev
readonly thistype next
private integer _count
method operator count takes nothing returns integer
return this.head._count
endmethod
static method createNode takes nothing returns thistype
local thistype this = thistype.allocate()
set this.head = 0
set this._first = 0
set this._last = 0
set this.prev = this
set this.next = this
set this._count = 0
return this
endmethod
static method createHead takes nothing returns thistype
local thistype this = thistype.createNode()
set this.head = this
return this
endmethod
method insertNode takes thistype node returns boolean
// Evitamos insertar si la instancia no es head
if ( this != this.head ) then
return false
endif
set node.head = this
set node.next = this
if ( this._first == 0 ) then
set this._first = node
endif
if ( this._last != 0 ) then
set node.prev = this._last
set this._last.next = node
endif
set this._last = node
set this._count = this._count + 1
return false
endmethod
method removeNode takes nothing returns boolean
local thistype head = this.head
if ( head == 0 ) then
return false
endif
if ( this == head ) then
return false
endif
if ( head._first == this ) then
set head._first = this.next
endif
if ( head._last == this ) then
set head._last = this.prev
endif
set this.prev.next = this.next
set this.next.prev = this.prev
set head._count = head._count - 1
call this.deallocate()
return true
endmethod
/**
* Removemos y destruimos todos los nodos de la lista
*/
method clearList takes nothing returns nothing
local thistype node
if ( this != this.head ) then
return
endif
loop
set node = this._first
exitwhen node == 0
call node.deallocate()
endloop
set this._first = 0
set this._last = 0
set this._count = 0
endmethod
endmodule
endlibrary[/jass]
Ejemplo[jass]scope DummyList initializer Init
private struct DummyList
integer i
implement DoublyLinkedList
endstruct
private function Init takes nothing returns nothing
local DummyList dummyList = DummyList.createHead()
local DummyList dummyNodeUno = DummyList.createNode()
local DummyList dummyNodeDos = DummyList.createNode()
set dummyNodeUno.i = 4
set dummyNodeDos.i = 8
call dummyList.insertNode(dummyNodeUno)
call dummyList.insertNode(dummyNodeDos)
call BJDebugMsg(I2S(dummyList.first.i)) // 4
call BJDebugMsg(I2S(dummyList.last.i)) // 8
endfunction
endscope[/jass]
Saludos.