Imports System
Imports System.Text
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim words() As String = _
{ "the", "fox", "jumped", "over", "the", "dog" }
Dim sentence As New LinkedList(Of String)(words)
Display(sentence)
Console.WriteLine("sentence.Contains(""jumped"") = {0}", _
sentence.Contains("jumped"))
' Add the word "today" to the beginning of the linked list.
' Remove the new node, and add it to the end of the list.
sentence.AddFirst("today")
Display(sentence)
Dim mark1 As LinkedListNode(Of String) = sentence.First
sentence.RemoveFirst()
sentence.AddLast(mark1)
Display(sentence)
sentence.RemoveLast()
sentence.AddLast("yesterday")
Display(sentence)
mark1 = sentence.Last
sentence.RemoveLast()
sentence.AddFirst(mark1)
Display(sentence)
sentence.RemoveFirst()
Dim current As LinkedListNode(Of String) = _
sentence.FindLast("the")
DisplayNode(current)
sentence.AddAfter(current, "old")
sentence.AddAfter(current, "lazy")
DisplayNode(current)
current = sentence.Find("fox")
DisplayNode(current)
sentence.AddBefore(current, "quick")
sentence.AddBefore(current, "brown")
DisplayNode(current)
' Keep a reference to the current node, "fox", and to the
' previous node in the list. Use the Find method to locate
' the node containing the value "dog". Show the position.
mark1 = current
Dim mark2 As LinkedListNode(Of String) = current.Previous
current = sentence.Find("dog")
DisplayNode(current)
' The AddBefore method throws an InvalidOperationException
' if you try to add a node that already belongs to a list.
Try
sentence.AddBefore(current, mark1)
Catch ex As InvalidOperationException
Console.WriteLine("Exception message: {0}", ex.Message)
End Try
' Remove the node referred to by mark1, and add it before
' the node referred to by current. Show the sentence,
' highlighting the position of the node referred to by
' current.
sentence.Remove(mark1)
sentence.AddBefore(current, mark1)
DisplayNode(current)
' Remove the node referred to by current. If you try to show
' its position now, the DisplayNode method prints a message.
' Add the node after the node referred to by mark2, and
' display the sentence, highlighting current.
sentence.Remove(current)
DisplayNode(current)
sentence.AddAfter(mark2, current)
DisplayNode(current)
' The Remove method finds and removes the first node that
' that has the specified value.
sentence.Remove("old")
Display(sentence)
' When the linked list is cast to ICollection(Of String),
' the Add method adds a node to the end of the list.
sentence.RemoveLast()
Dim icoll As ICollection(Of String) = sentence
icoll.Add("rhinoceros")
Display(sentence)
' Create an array, specifying a size one less than the size
' of the linked list, because Visual Basic allocates one
' more element than you specify.
Console.WriteLine(vbLf & "Copy the list to an array.")
Dim sArray(sentence.Count - 1) As String
sentence.CopyTo(sArray, 0)
For Each s As String In sArray
Console.WriteLine(s)
Next
' Release all the nodes.
sentence.Clear()
End Sub
Private Shared Sub Display(ByVal words As LinkedList(Of String))
For Each word As String In words
Console.Write(word & " ")
Next
Console.WriteLine()
End Sub
Private Shared Sub DisplayNode(ByVal node As LinkedListNode(Of String))
If node.List Is Nothing Then
Console.WriteLine("Node ""{0}"" is not in a list.", node.Value)
Return
End If
Dim result As New StringBuilder ("(" & node.Value & ")")
Dim nodeP As LinkedListNode(Of String) = node.Previous
While nodeP IsNot Nothing
result.Insert(0, nodeP.Value & " ")
nodeP = nodeP.Previous
End While
node = node.Next
While node IsNot Nothing
result.Append(" " & node.Value)
node = node.Next
End While
Console.WriteLine(result)
End Sub
End Class
'This code example produces the following output:
'
'the fox jumped over the dog
'sentence.Contains("jumped") = True
'today the fox jumped over the dog
'the fox jumped over the dog today
'the fox jumped over the dog yesterday
'yesterday the fox jumped over the dog
'the fox jumped over (the) dog
'the fox jumped over (the) lazy old dog
'the (fox) jumped over the lazy old dog
'the quick brown (fox) jumped over the lazy old dog
'the quick brown fox jumped over the lazy old (dog)
'Exception message: The LinkedList node belongs a LinkedList.
'the quick brown jumped over the lazy old fox (dog)
'Node "dog" is not in a list.
'the quick brown (dog) jumped over the lazy old fox
'the quick brown dog jumped over the lazy fox
'the quick brown dog jumped over the lazy rhinoceros
'
'Copy the list to an array.
'the
'quick
'brown
'dog
'jumped
'over
'the
'lazy
'rhinoceros