Linked List

Ordered linked list.


BET_LinkedNode class

Private Value As Variant 
Private NextNode As BET_LinkedNode

BET_LinkedList class

Private head As BET_LinkedNode 

Public Sub Add(value As Variant)
Dim node As BET_LinkedNode
        
    Set node = New BET_LinkedNode
    node.value = value
    Set node.nextNode = head
    Set head = node
End Sub

Public Sub Remove(value As Variant)
Dim node As BET_LinkedNode
Dim prev As BET_LinkedNode
    
    Set node = head
    While Not node Is Nothing
        If node.value = value Then
'remove node
            If node Is head Then
                Set head = node.nextNode
            Else
                Set prev.nextNode = node.nextNode
            End If
            Exit Sub
        End If
        Set prev = node
        Set node = node.nextNode
    Wend
End Sub

Public Function Exists(value As Variant) As Boolean
Dim node As BET_LinkedNode
    
    Set node = head
    While Not node Is Nothing
        If node.value = value Then
            Exists = True
            Exit Function
        End If
        Set node = node.nextNode
    Wend
End Function

Public Function Count() As Long
Dim node As BET_LinkedNode
    
    Set node = head
    While Not node Is Nothing
        Count = Count + 1
        Set node = node.nextNode
    Wend
End Function

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext