Linked List

Ordered linked list.


BET_ListItem class

Private TheValue As Variant 
Private NextNode As BET_ListItem

BET_LinkedList class

Private head As BET_ListItem 

Public Sub Add(value As Variant)
Dim node As BET_ListItem
Dim lCurrent As BET_ListItem
Dim lPrevious As BET_ListItem
        
   Set node = New BET_ListItem
   node.TheValue = value

   all Fun_Search(value, lCurrent, lPrevious)

   Set node.NextNode = head
   Set head = node
End Sub

Public Sub Remove(value As Variant)
Dim node As BET_ListItem
Dim prev As BET_ListItem
    
    Set node = head
    While Not node Is Nothing
        If node.TheValue = 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_ListItem
    
    Set node = head
    While Not node Is Nothing
        If node.TheValue = value Then
            Exists = True
            Exit Function
        End If
        Set node = node.NextNode
    Wend
End Function

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

Private Function Fun_Search(ByVal varItem As Variant, 
                            ByRef lCurrent As BET_ListItem, _
                            ByRef lPrevious As BET_ListItem)
Dim bFound As Boolean
   bFound = False
   Set lPrevious = Nothing
   Set lCurrent = _Head

   Do While Not lCurrent Is Nothing
      If (varItem > lCurrent.Value) Then
         Set lPrevious = lCurrent
         Set lCurrent = lCurrent.NetItem
      Else
         Exit Do
      End If
   Loop

   If Not (lCurrent Is Nothing) Then
      bFound = (lCurrent.Value = varItem)
   End If
   Search = bFound
End Function

Private Function Delete()

End Function

Sample

Public Sub Testing_LinkedList() 
Dim myLinkedList As BET_LinkedList
   Set myLinkedList = New BET_LinkedList
   myLinkedList.Add("Mon")
   Debug.Print myLinkedList.Count
   myLinkedList.Add("Tue")
   myLinkedList.Add("Wed")
   Debug.Print myLinkedList.Exists("Tue")
   myLinkedList.Remove("Tue")
End Sub

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