For many years, I worked with Visual Basic convinced that it was impossible to create a
linked list using VB. It therefore came as a great (and pleasant) surprise to discover that
this is not at all the case. I thought therefore that I would share this technique with the
noders of Everything.
If you got this far, then chances are that you already know that VB has classes and
passes variables by reference unless instructed otherwise. These two facts lie at the heart
of the VB Linked list. I'll start with the node class which I will call llNode.
An llNode is simply a vehicle for storing an item of information. In this case, I will
make the information a Long Integer. However the information can be anything from a boolean
(why?) (don't answer that) to an instance of a
class.
to create a VB Linked List, create a Class Module called llNode and insert the following
code. note that this is just one of many methods of constructing a VB linked
list.
Code for creating an llNode
Option Compare Database
Option Explicit
'llNode Class, the basic element of a VB Linked List
'pInformation: the data we plan to manipulate.
Private pInformation As Variant
'pNextllNode: makes this a Linked List
Private pNextllNode As llNode
'pPreviousllNode: makes this a Doubly Linked List thus, you can move both forward and
backward in the list.
Private pPreviousllNode As llNode
'We need to be able to set and get the information in the node, so we have the
setInformation and getInformation functions.
Property Let Information(paramInformation As Long)
pInformation = paramInformation
End Property
Property Get Information() As Long
Information = pInformation
End Property
'It is also necessary to get the NextllNode and the PreviousllNode variables.
Property Get NextllNode() As llNode
Set NextllNode = pNextllNode
End Property
Property Get PreviousllNode() As llNode
Set PreviousllNode = pPreviousllNode
End Property
'Property lets wont work for setting pNextllNode and pPreviousllNode so a pair of
functions will do the job.
Public Function setNextLLNode(paramllNode As llNode)
Set pNextllNode = paramllNode
End Function
Public Function setPreviousllNode(paramllNode As llNode)
Set pPreviousllNode = paramllNode
End Function
'The only remaining function is to insert new nodes to the list. This will be done
by
'handing a new node to the first node in the list. If the new nodes value is smaller
than the existing node,
'the new node is inserted in front of the existing one. Otherwise it is handed on to the
'next node in the list until it is inserted (or comes to the end of the list, in which
case
'it is inserted behind the last node.
Public Function doInsert(paramllNode As llNode)
If paramllNode.Information < pInformation Then
Call paramllNode.setNextLLNode(Me)
If Not pPreviousllNode Is Nothing Then
Call
paramllNode.setPreviouslLNode(Me.PreviousllNode)
Call
Me.PreviousllNode.setNextLLNode(paramllNode)
End If
Set pPreviousllNode = paramllNode
ElseIf pNextllNode Is Nothing Then
Set pNextllNode = paramllNode
Call pNextllNode.setPreviouslLNode(Me)
Else
Call pNextllNode.doInsert(paramllNode)
End If
End Function
The LinkedList Class
Having created the llNode class, we need to create the LinkedList class which is simply
an object that points at the first node in the linked list. When you wish to add an llNode
to a linked list, simply hand the new node to the linked list object. If there are no
llNode objects in the list, the LinkedList object will set the new llNode as the first item
in the list.
Otherwise, it will hand the new llNode to the first llNode in the list and let the llNode
objects sort out for themselves where it should go. In this instance this is determined by comparing the value of pInformation in the new node to pInformation in the old node.
This comparison should change depending upon how you want the linked list to order itself.
to create the LinkedList object, create a new class module called (you guessed it)
"LinkedList" and insert the following code.
Option Compare Database
Option Explicit
'LinkedList Class, a sentinel for the llNode Class
'pNextllNode: points to the first node in the list.
Private pNextllNode As llNode
'It is necessary to set and to get the NextllNode variable.
Property Let NextllNode(paramllNode As llNode)
pNextllNode = paramllNode
End Property
Property Get NextllNode() As llNode
Set NextllNode = pNextllNode
End Property
'The only purpose of the LinkedList class is to point to the first node in the list
and
'to pass new nodes directly to the first node in the list when a new node must be
added to the list.
Public Sub doInsert(paramllNode As llNode)
If pNextllNode Is Nothing Then
Set pNextllNode = paramllNode
Else
Call pNextllNode.doInsert(paramllNode)
If Not pNextllNode.PreviousllNode Is Nothing
Then
Set pNextllNode =
pNextllNode.PreviousllNode
End If
End If
End Sub
Testing the Linked List
And that's how to create a linked list in Visual Basic. One last piece of code, this
time to test the thing and
- Prove that it works correctly
- Show you how it works
What this does is create nodes containing the odd numbers from 1 to 9 in ascending order
then the even numbers in descending order adding each node to the Linked List as it is
created. Having done so, it lists the values of the nodes in the list as they come out of
the list using the debug.print command. If I've written this correctly they should come out
in order from 1 to 10.
Option Compare Database
Option Explicit
Public Function testlist()
Dim LL As LinkedList
Dim theNode As llNode
Dim cycle As Long
Set LL = New LinkedList
For cycle = 1 To 10 Step 2
Set theNode = New llNode
theNode.Information = cycle
Call LL.doInsert(theNode)
Next
For cycle = 10 To 1 Step -2
Set theNode = New llNode
theNode.Information = cycle
Call LL.doInsert(theNode)
Next
Set theNode = LL.NextllNode
Do Until theNode.NextllNode Is Nothing
Debug.Print theNode.Information
Set theNode = theNode.NextllNode
Loop
Debug.Print theNode.Information
End Function
Try it out for yourself!