chamsi09

Untitled

Dec 12th, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.13 KB | None | 0 0
  1. Module Module1
  2.     Sub Main()
  3.         ' Create a new LinkedList of Integer
  4.         ' LinkedList is a doubly-linked list, allowing elements to be efficiently added or removed from both ends or at specific positions.
  5.         Dim numbers As New LinkedList(Of Integer)
  6.  
  7.         ' Adding elements to the LinkedList
  8.         numbers.AddLast(2)   ' AddLast adds the element to the end of the LinkedList.
  9.         numbers.AddFirst(1)  ' AddFirst adds the element to the beginning of the LinkedList.
  10.         Console.WriteLine("Count after additions: " & numbers.Count) ' Count returns the total number of elements in the LinkedList.
  11.  
  12.         ' Adding more elements
  13.         Dim nodeThree As LinkedListNode(Of Integer) = numbers.AddLast(3) ' Adds the value 3 to the end and returns the newly created node.
  14.         Dim nodeFour As LinkedListNode(Of Integer) = numbers.AddLast(4) ' Adds the value 4 to the end and returns the newly created node.
  15.  
  16.         ' Inserting a node before an existing node
  17.         numbers.AddBefore(nodeFour, 5)  ' Adds the value 5 before nodeFour (which contains 4).
  18.  
  19.         ' Inserting a node after the first node
  20.         numbers.AddAfter(numbers.First, 0)  ' Adds the value 0 after the first node (which contains 1).
  21.  
  22.         ' Check if the LinkedList contains a specific value
  23.         Console.WriteLine("Contains 3: " & numbers.Contains(3))  ' Checks if the value 3 exists in the LinkedList.
  24.  
  25.         ' Removing an element by value
  26.         numbers.Remove(0)  ' Removes the first occurrence of the value 0 from the LinkedList.
  27.  
  28.         ' Remove the first and last elements
  29.         numbers.RemoveFirst() ' Removes the first node from the LinkedList.
  30.         numbers.RemoveLast()  ' Removes the last node from the LinkedList.
  31.  
  32.         ' Accessing the first and last nodes of the LinkedList
  33.         Dim firstNode As LinkedListNode(Of Integer) = numbers.First  ' Retrieves the first node in the LinkedList.
  34.         Dim lastNode As LinkedListNode(Of Integer) = numbers.Last    ' Retrieves the last node in the LinkedList.
  35.         Console.WriteLine("First Node Value: " & firstNode.Value)    ' Accesses the value stored in the first node.
  36.         Console.WriteLine("Last Node Value: " & lastNode.Value)      ' Accesses the value stored in the last node.
  37.  
  38.         ' Traversing the list using the Next property
  39.         ' Start from the first node and move to the next node repeatedly until the end of the list is reached.
  40.         Console.WriteLine("Traversing the list:")
  41.         Dim currentNode As LinkedListNode(Of Integer) = firstNode
  42.         While currentNode IsNot Nothing
  43.             Console.Write(currentNode.Value & " ") ' Print the value of the current node.
  44.             currentNode = currentNode.Next         ' Move to the next node in the list.
  45.         End While
  46.         Console.WriteLine()
  47.  
  48.         ' Final output of the LinkedList using a For Each loop
  49.         ' The For Each loop iterates through all the values in the LinkedList.
  50.         Console.WriteLine("Final State of LinkedList:")
  51.         For Each num In numbers
  52.             Console.WriteLine(num) ' Print each value in the LinkedList.
  53.         Next
  54.     End Sub
  55. End Module
  56.  
Advertisement
Add Comment
Please, Sign In to add comment