User:Eriksiers/Linked List: Difference between revisions

Fixed old lang -> syntaxhighlight error
(created)
 
(Fixed old lang -> syntaxhighlight error)
 
(2 intermediate revisions by the same user not shown)
Line 1:
This is a [[linked list]] class for [[:Category:Visual Basic|Visual Basic]] & [[:Category:VBA|VBA]]. It should work at least as far back as VB4.
 
<langsyntaxhighlight lang="vb">'This class is in the Public Domain.
Private Type Node
data As Variant
prevnPrv As Variant
nextnNxt As Variant
End Type
 
Line 58:
listEmpty = False
curNode = 0
nodes(0).prevnPrv = 0
nodes(0).nextnNxt = 0
Else
Dim tmp As Variant
tmp = UBound(nodes) + 1
ReDim Preserve nodes(tmp)
'this If block splices the new node into the list
If Before = where Then
nodes(nodes(curNode).nextnNxt).prevnPrv = tmp
nodes(tmp).nextnNxt = nodes(curNode).nextnNxt
nodes(tmp).prevnPrv = curNode
nodes(curNode).nextnNxt = tmp
Else
nodes(nodes(curNode).prevnPrv).nextnNxt = tmp
nodes(tmp).nextnNxt = curNode
nodes(tmp).prevnPrv = nodes(curNode).prevnPrv
nodes(curNode).prevnPrv = tmp
End If
curNode = tmp
Line 89:
If UBound(nodes) > 0 Then
'patch the surrounding elements together
nodes(nodes(curNode).prevnPrv).nextnNxt = nodes(curNode).nextnNxt
nodes(nodes(curNode).nextnNxt).prevnPrv = nodes(curNode).prevnPrv
'select the appropriate nextnNxt item
curNode = nodes(curNode).nextnNxt
'finally, the actual delete
ReDim Preserve nodes(UBound(nodes) - 1)
Line 103:
Else
'patch the surrounding elements together
nodes(nodes(curNode).prevnPrv).nextnNxt = nodes(curNode).nextnNxt
nodes(nodes(curNode).nextnNxt).prevnPrv = nodes(curNode).prevnPrv
'swap with node at end of list
Dim tmp As Node
Line 111:
nodes(curNode) = tmp
'patch the list
nodes(nodes(curNode).prevnPrv).nextnNxt = curNode
nodes(nodes(curNode).nextnNxt).prevnPrv = curNode
'select the appropriate nextnNxt item
If UBound(nodes) <> nodes(UBound(nodes)).nextnNxt Then curNode = nodes(UBound(nodes)).nextnNxt
'finally, the actual delete
ReDim Preserve nodes(UBound(nodes) - 1)
Line 123:
 
Public Function nextnode() As Boolean
If curNode <= UBoundnodes(nodescurNode) Then.nNxt
curNode = curNode + 1
Else
curNode = 0
End If
nextnode = True
End Function
 
Public Function prevNode() As Boolean
If curNode >= 0 Thennodes(curNode).nPrv
End Function</langsyntaxhighlight>
curNode = curNode - 1
Else
curNode = UBound(nodes)
End If
prevNode = True
End Function</lang>
 
==Simplified==
Line 146 ⟶ 136:
With minor modifications, this could conceivably work under QBasic (though obviously not as a class).
 
<langsyntaxhighlight lang=vb>'This class is in the Public Domain.
Public Enum BeforeOrAfter
Before = 0
Line 243 ⟶ 233:
End If
prevNode = True
End Function</langsyntaxhighlight>
1,150

edits