VB.Net Tutorial/Collections/Your List
Define your own linked list
<source lang="vbnet">Public Class List
Private firstNode As Node Private lastNode As Node Private name As String Public Sub New(ByVal listName As String) name = listName firstNode = Nothing lastNode = Nothing End Sub Public Sub New() MyClass.New("list") End Sub " New Public Sub InsertAtFront(ByVal insertItem As Object) SyncLock (Me) If IsEmpty() Then lastNode = New Node(insertItem) firstNode = lastNode Else firstNode = New Node(insertItem, firstNode) End If End SyncLock End Sub Public Sub InsertAtBack(ByVal insertItem As Object) SyncLock (Me) If IsEmpty() Then lastNode = New Node(insertItem) firstNode = lastNode Else lastNode.NextNode = New Node(insertItem) lastNode = lastNode.NextNode End If End SyncLock End Sub Public Function RemoveFromFront() As Object SyncLock (Me) Dim removeItem As Object = Nothing If IsEmpty() Then Throw New EmptyListException(name) End If removeItem = firstNode.Data If firstNode Is lastNode Then firstNode = Nothing lastNode = Nothing Else firstNode = firstNode.NextNode End If Return removeItem End SyncLock End Function Public Function RemoveFromBack() As Object SyncLock (Me) Dim removeItem As Object = Nothing If IsEmpty() Then Throw New EmptyListException(name) End If removeItem = lastNode.Data If firstNode Is lastNode Then lastNode = Nothing firstNode = lastNode Else Dim current As Node = firstNode While (Not (current.NextNode Is lastNode)) current = current.NextNode End While lastNode = current current.NextNode = Nothing End If Return removeItem End SyncLock End Function Public Function IsEmpty() As Boolean SyncLock (Me) If firstNode Is Nothing Then Return True Else Return False End If End SyncLock End Function Public Overridable Sub Print() SyncLock (Me) If IsEmpty() Then Console.WriteLine("Empty " & name) Return End If Console.Write("The " & name & " is: ") Dim current As Node = firstNode While Not current Is Nothing Console.Write(current.Data & " ") current = current.NextNode End While Console.WriteLine(vbCrLf) End SyncLock End Sub
End Class Public Class Node
Private mData As Object Private mNextNode As Node Public Sub New(ByVal dataValue As Object) MyClass.New(dataValue, Nothing) End Sub Public Sub New(ByVal dataValue As Object, ByVal nextNodeValue As Object) mData = dataValue mNextNode = nextNodeValue End Sub Public ReadOnly Property Data() As Object Get Return mData End Get End Property Public Property NextNode() As Node Get Return mNextNode End Get Set(ByVal value As Node) mNextNode = value End Set End Property
End Class Public Class EmptyListException
Inherits ApplicationException Public Sub New(ByVal name As String) MyBase.New("The " & name & " is empty") End Sub " New
End Class Module Tester
Sub Main() Dim list As List = New List() list.InsertAtFront(True) list.InsertAtFront("A"c) list.InsertAtBack(34567) list.InsertAtBack("hello") list.Print() Dim removedObject As Object Try removedObject = list.RemoveFromFront() Console.WriteLine(Convert.ToString(removedObject) & " removed")
removedObject = list.RemoveFromBack() Console.WriteLine(Convert.ToString(removedObject) & " removed") list.Print() Catch emptyListException As EmptyListException Console.Error.WriteLine(vbCrLf & Convert.ToString(emptyListException)) End Try End Sub
End Module</source>
The list is: A True 34567 hello A removed hello removed The list is: True 34567