A linked list is a container element a bit similar to an array, but differently working. An array is a block of memory split up in many parts which is reserved when it is created, or redimensioned. A linked list doesn't have to be redimensioned: New elements are created elsewhere in the memory and linked to each other (every element contains a reference to the next and, in case of a double-linked list, to the previous element). This way we can create and delete elements when- and whereever we want to.
You create a linked list with
Set ll = New LinkedList
If you don't use the BackInserter, you can add an item at the back of the
list with
itemRef = ll.AddBack( valueorobject )
itemRef = ll.AddFront( valueorobject )
itemRef = ll.Insert( position, valueorobject )
itemRef.Delete
or ll.Delete ( index )
valueorobjectvar = ll( index )
valueorobjectvar = ll.Item( index )
ll.Item( index ) = value
Set ll.Item( index ) = object
The number of items in the list is returned with
var = ll.Count
If you add an array as an item to a linked list, you can't access it normally with
ll( index, ArrayIndex )
ll( index ).Item( AryIndex )
Basically, the original iterator is an abstraction of a pointer for to point inside containers. This iterator however doesn't have the original pointer interface for obvious reasons (VBS doesn't have any pointers or overloadable operators.)
It is useful for performing actions on a sequence of items in a LinkedList. Most basic example is
myIterator = ll.Begin
lastIterator= ll.Last
Do Until myIterator.Object Is lastIterator.Object
Debug.DebugMsg myIterator.Item
myIterator.Iterate()
Loop
You first have to get an iterator with
it = ll.Begin
it = ll.Last
it = ll.RBegin
it = ll.RLast
it = ll.AtIt ( index )
You get the selected item with it.Item
and the selected LinkedList
object (needed for equivalency comparison) with it.Object
.
it.Delete()
deletes the current item and proceeds to the next one.
With it.Iterate()
you proceed forward in the list, with
it.Iterate()
you step backwards. Both of the functions return a copy
of the iterator after the operation. Important: For going through the list backwards,
you need IterateBack()
, even though the end of the list is in RBegin and the start
in RLast. This may change in future versions, if a direction flag is implemented.
You get a copy of the iterator through normal assignment (without Set): it2 =
it1
or with it2 = it1.GetCopy
bi = ll.BackInserter()
. Afterwards you can add items to
the list with
bi.Item = valueorobject1
bi.Item = valueorobject2
bi.Item = valueorobject3
bi2 = bi1
or explicitly with bi2 = bi1.GetCopy
.