Linked List VB.NET Implementation

The following VB.NET code is code illustrates the various operations of a linked list:

 

Public Class Form1
    Dim aListData(9) As String
    Dim aNext(9) As Integer
    Dim iStart As Integer = 1
    Dim iNextFree As Integer = 1

    Private Sub btnMakeList_Click(sender As Object, e As EventArgs) Handles btnMakeList.Click
        'hard coded creation of a linked list for testing only

        aListData(1) = "chloe"
        aListData(2) = "francis"
        aListData(3) = "beatrix"
        aListData(4) = "david"
        aListData(5) = "edward"
        aListData(6) = "abigail"

        aNext(1) = 4
        aNext(2) = 0
        aNext(3) = 1
        aNext(4) = 5
        aNext(5) = 2
        aNext(6) = 3

        iStart = 6
        iNextFree = 7

    End Sub

    Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
        'view the contents of the linked list pointers

        Dim x As Integer
        Dim stOut As String

        stOut = "Data" & "  " & "next" & vbNewLine & vbNewLine

        For x = 1 To 9
            stOut = stOut & x & "   " & aListData(x) & "  " & aNext(x) & vbNewLine
        Next
        MsgBox("start=" & iStart & " next free=" & iNextFree & vbNewLine & stOut)
    End Sub

    Private Sub btnTraverse_Click(sender As Object, e As EventArgs) Handles btnTraverse.Click
        Call TraverseList()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        MsgBox(SearchList(Me.txtFind.Text))
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        Call RemoveFromLinkedList(Me.txtRemove.Text)
        Me.txtRemove.Text = ""
        Me.txtRemove.Select()
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Call AddToLinkedList(Me.txtItem.Text)
        Me.txtItem.Text = ""
        Me.txtItem.Select()
    End Sub

    Sub TraverseList()
        'visit each item in the list by following the pointers
        Dim ptr As Integer
        ptr = iStart
        Do Until ptr = 0
            MsgBox(aListData(ptr))
            ptr = aNext(ptr)
        Loop
    End Sub

    Function SearchList(FindMe As String) As Boolean
        'search for an item in a linked list
        Dim bFound As Boolean
        Dim ptr As Integer

        ptr = iStart

        Do Until ptr = 0
            If aListData(ptr) = FindMe Then
                bFound = True
                Exit Do
            End If
            ptr = aNext(ptr)
        Loop
        Return bFound
    End Function

    Sub RemoveFromLinkedList(ItemToRemove As String)
        'remove an item from a linked list
        Dim ptr As Integer
        Dim prevPtr As Integer

        ptr = iStart
        prevPtr = iStart

        'if item to remove is the start, the item it was pointing to becomes the new start
        If aListData(ptr) = ItemToRemove Then
            iStart = aNext(ptr)
            Exit Sub
        End If

        'otherwise traverse the list
        Do Until ptr = 0
            If aListData(ptr) = ItemToRemove Then
                'set preceding item's pointer to the item this was pointing to
                aNext(prevPtr) = aNext(ptr)
                Exit Do
            End If
            prevPtr = ptr
            ptr = aNext(ptr)
        Loop
    End Sub

    Sub AddToLinkedList(NewItem As String)
        'add an item to a linked list
        Dim ptr As Integer
        Dim prevPtr As Integer

        ptr = iStart
        prevPtr = iStart

        aListData(iNextFree) = NewItem
        aNext(iNextFree) = 0

        'handle the very first node separately
        If iNextFree = 1 Then
            iNextFree = iNextFree + 1
            Exit Sub
        End If

        'if not first node, check where it belongs
        Do Until ptr = 0
            If NewItem < aListData(ptr) Then
                aNext(iNextFree) = ptr
                If ptr = iStart Then
                    iStart = iNextFree
                Else
                    aNext(prevPtr) = iNextFree
                End If
                Exit Do
            Else
                prevPtr = ptr
                ptr = aNext(ptr)
            End If
        Loop

        'new node must belong at the end of the list
        If ptr = 0 Then
            aNext(prevPtr) = iNextFree
        End If

        iNextFree = iNextFree + 1
    End Sub

End Class