Check linked list is circular or not in vb.net
Vb program for Check linked list is circular or not . Here more information.
' Include namespace system
Imports System
' Vb.net Program for
' Check linked list is circular or not
' 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
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)
if (ll.isCircular()) Then
Console.WriteLine("Yes")
Else
Console.WriteLine("No")
End IF
' Connect last node to head
ll.head.[next].[next].[next].[next].[next] = ll.head
if (ll.isCircular()) Then
Console.WriteLine("Yes")
Else
Console.WriteLine("No")
End IF
End Sub
End Class
Output
No
Yes
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