Skip to main content

Convert singly linked list to circular list in vb.net

Vb program for Convert singly linked list to circular list. Here problem description and other solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Convert singly linked list into circular 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)  
        Me.data = data
        Me.next = Nothing
    End Sub
End Class

public Class LinkedList 
    Public  head As LinkNode
    '  Class constructor
    Public Sub New() 
        Me.head = Nothing
    End Sub
    '  Check circular linked list or not
    '  Note that this function is not capable to detect loop
    Public Function  isCircular() As Boolean
        if (Me.head  Is  Nothing) Then 
            '  Case when linked list is empty
            Return False
        Else 
            Dim temp As LinkNode = Me.head
            while (temp IsNot Nothing) 
                '  Visit to next node
                temp = temp.[next]
                if (temp  Is  Me.head) Then 
                    '  When detecting circular node
                    Return True
                End If
            End While
            '  When not circular linked list
            Return False
        End IF

    
    End Function 
    '  Display node element of linked list
    Public Sub display()
        if (Me.head  Is  Nothing) Then 
            Console.WriteLine("Empty Linked List")
        Else 
            Console.Write("Linked List Element :")
            Dim temp As LinkNode = Me.head
            '  Iterate linked list
            while (temp IsNot Nothing) 
            
                '  Display node
                Console.Write("  " + temp.data.ToString())
                '  Visit to next node
                temp = temp.[next]
                if (temp  Is  Me.head) Then 
                    '  Stop iteration
                    Return
                End If

            End While
            Console.WriteLine()
        End IF

    
    End Sub
    '  Coverted circular Linked list
    Public Sub makeCircular()
        if (Me.head  Is  Nothing) Then 
            Console.WriteLine("Empty Linked List")
        Else 
            Dim temp As LinkNode = Me.head
            '  Find last node  
            while (temp.[next] IsNot Nothing) 
            
                temp = temp.[next]
                if (temp  Is  Me.head) Then 
                    '  Already circular Linked list
                    Return
                End If

            End While
            '  Connect last node to first node
            temp.[next] = Me.head
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim ll As LinkedList = New LinkedList()
        '  Insert element of linked list
        ll.head = New LinkNode(1)
        ll.head.[next] = New LinkNode(2)
        ll.head.[next].[next] = New LinkNode(3)
        ll.head.[next].[next].[next] = New LinkNode(4)
        ll.head.[next].[next].[next].[next] = New LinkNode(5)
        ll.head.[next].[next].[next].[next].[next] = New LinkNode(6)
        ll.head.[next].[next].[next].[next].[next].[next] = New LinkNode(7)
        ll.display()
        if (ll.isCircular()) Then 
            Console.WriteLine("Circular Yes")
        Else 
            Console.WriteLine("Circular No")
        End IF

        Console.WriteLine("After Convert")
        ll.makeCircular()
        if (ll.isCircular()) Then 
            Console.WriteLine("Circular Yes")
        Else 
            Console.WriteLine("Circular No")
        End IF

    
    End Sub

End Class

Output

Linked List Element :  1  2  3  4  5  6  7
Circular No
After Convert
Circular Yes




Comment

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