Skip to main content

Find length of circular linked list in

Vb program for Find length of circular linked list . Here more information.

' Include namespace system
Imports System 
' 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)
        = data = 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
            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
        '  Display result
    End Sub

End Class




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