Posted on by Kalkicode
Code Single linked list

Check if two linked lists are identical or not in vb.net

Vb program for Check if two linked lists are identical or not. Here more information.

' Include namespace system
Imports System 
'    Vb.net program for
'    Check identical linked lists

'  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 SingleLL 
    Public  head As LinkNode
    Public  tail As LinkNode
    
    Public Sub New()
        
        Me.head = Nothing
        Me.tail = Nothing
    
    End Sub
    '  Add new Node at end of linked list 
    Public Sub addNode(ByVal data As Integer)
        Dim node As LinkNode = New LinkNode(data)
        if (Me.head  Is  Nothing) Then 
            Me.head = node
        Else 
            '  Append the node at last position
            Me.tail.[next] = node
        End IF

        Me.tail = node
    
    End Sub
    '  Display linked list element
    
    Public Sub display()
        if (Me.head  Is  Nothing) Then 
            Return
        End If

        Dim temp As LinkNode = Me.head
        '  iterating linked list elements
        
        while (temp IsNot Nothing) 
        
            Console.Write(" " + temp.data.ToString() + " →")
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.WriteLine(" NULL")
    
    End Sub
    
    Public Sub isIdentical(ByVal other As SingleLL)
        Dim t1 As LinkNode = Me.head
        Dim t2 As LinkNode = other.head
        '  Compare linked list node
        
        while (t1 IsNot Nothing AndAlso 
               t2 IsNot Nothing AndAlso t1.data = t2.data) 
        
            '  Visit to next node
            t1 = t1.[next]
            t2 = t2.[next]
        End While
        Console.WriteLine(vbLf & " Given Lists ")
        Me.display()
        other.display()
        if (t1 IsNot Nothing OrElse t2 IsNot Nothing) Then 
            Console.WriteLine(" Is not Identical")
        Else 
            Console.WriteLine(" Is Identical")
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim sll1 As SingleLL = New SingleLL()
        Dim sll2 As SingleLL = New SingleLL()
        Dim sll3 As SingleLL = New SingleLL()
        Dim sll4 As SingleLL = New SingleLL()
        '  First linked list
        '   1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
        sll1.addNode(1)
        sll1.addNode(2)
        sll1.addNode(5)
        sll1.addNode(4)
        sll1.addNode(3)
        sll1.addNode(6)
        sll1.addNode(7)
        '  Second linked list
        '   1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
        sll2.addNode(1)
        sll2.addNode(2)
        sll2.addNode(5)
        sll2.addNode(4)
        sll2.addNode(3)
        sll2.addNode(6)
        sll2.addNode(7)
        '  Third linked list
        '   1 → 2 → 5 → 4 → 3 → 6 → NULL
        sll3.addNode(1)
        sll3.addNode(2)
        sll3.addNode(5)
        sll3.addNode(4)
        sll3.addNode(3)
        sll3.addNode(6)
        '  Fourth linked list
        '   11 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
        sll4.addNode(11)
        sll4.addNode(2)
        sll4.addNode(5)
        sll4.addNode(4)
        sll4.addNode(3)
        sll4.addNode(6)
        sll4.addNode(7)
        '  Test 
        sll1.isIdentical(sll2)
        sll1.isIdentical(sll3)
        sll2.isIdentical(sll4)
    
    End Sub

End Class

Output

 Given Lists
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 Is Identical

 Given Lists
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 1 → 2 → 5 → 4 → 3 → 6 → NULL
 Is not Identical

 Given Lists
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 11 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 Is not Identical

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