Find length of circular linked list in vb.net
Vb program for Find length of circular linked list . Here more information.
' Include namespace system
Imports System
' Vb.net program for
' Count number of nodes in circular linked list
' Define class of linked list Node
Public Class LinkNode
Public data As Integer
Public [next] As LinkNode
Public Sub New(ByVal data As Integer, ByVal first As LinkNode)
Me.data = data
Me.next = first
End Sub
End Class
public Class CircularLinkedList
Public head As LinkNode
' Class constructor
Public Sub New()
Me.head = Nothing
End Sub
' Insert node at end of circular linked list
Public Sub insert(ByVal value As Integer)
' Create a new node
Dim node As LinkNode = New LinkNode(value, Me.head)
if (Me.head Is Nothing) Then
' First node of linked list
Me.head = node
node.[next] = Me.head
Else
Dim temp As LinkNode = Me.head
' Find the last node
while (temp.[next] IsNot Me.head)
' Visit to next node
temp = temp.[next]
End While
' Add new node at the last
temp.[next] = node
End IF
End Sub
Public Function countNode() As Integer
if (Me.head Is Nothing) Then
Return 0
End If
' Start with second node
Dim temp As LinkNode = Me.head.[next]
' This is used to count linked node
Dim count As Integer = 1
' iterate circular linked list
while (temp IsNot Me.head)
count += 1
' Visit to next node
temp = temp.[next]
End While
Return count
End Function
Public Shared Sub Main(ByVal args As String())
Dim cll As CircularLinkedList = New CircularLinkedList()
' Add nodes
cll.insert(1)
cll.insert(3)
cll.insert(5)
cll.insert(7)
cll.insert(9)
cll.insert(11)
' Display result
Console.WriteLine(cll.countNode())
End Sub
End Class
Output
6
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment