Skip to main content

Clone a linked list with random fields in vb.net

Vb program for Clone a linked list with random fields. Here more information.

' Include namespace system
Imports System 
'  Vb.net Program
'  Clone a linked list with next and random field

'  Linked List Node
Public Class LinkNode
    Public  data As Integer
    Public  [next] As LinkNode
    Public  random As LinkNode
    Public Sub New(ByVal data As Integer)
        '  Set node value
        Me.data = data
        Me.next = Nothing
        Me.random = Nothing
    End Sub
End Class

Public Class MyLinkedList
    Public  head As LinkNode
    Public Sub New()
        Me.head = Nothing
    End Sub
    '  Add new node at the end of linked list
    Public Sub insert(ByVal value As Integer)
        '  Create  node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            Me.head = node
        Else
            Dim temp As LinkNode = Me.head
            '  Find last node
            while (temp.[next] IsNot Nothing)
                '  Visit to next node
                temp = temp.[next]
            End While
            '  Add node at last position
            temp.[next] = node
        End IF
    End Sub
    '  Display linked list element
    Public Sub display()
        if (Me.head  Is  Nothing) Then
            Console.WriteLine( vbLf &"Empty linked list")
            Return
        End If
        Dim temp As LinkNode = Me.head
        Console.Write( vbLf &" ")
        while (temp IsNot Nothing)
            if (temp IsNot Me.head) Then
                Console.Write("--->")
            End If
            if (temp.random IsNot Nothing) Then
                Console.Write(temp.data.ToString() + 
                   "(" + temp.random.data.ToString() + ")")
            Else
                Console.Write(temp.data)
            End IF
            temp = temp.[next]
        End While
        Console.WriteLine("-->NULL")
    End Sub
    '  Clone the linked list  with next and random field
    Public Function  cloneList() As LinkNode
        if (Me.head  Is  Nothing) Then
            Return Nothing
        End If
        '  Define some auxiliary variable
        Dim temp As LinkNode = Nothing
        Dim current As LinkNode = Me.head
        Dim node As LinkNode = Nothing
        Dim front As LinkNode = Nothing
        '  Step 1
        '  Create and combine clone node
        while (current IsNot Nothing)
            ' Get current node of actual linked list
            temp = current
            '  Create clone node
            node = New LinkNode(current.data)
            '  Visit to next node
            current = current.[next]
            '  Connect clone node after actual node
            temp.[next] = node
            '  Binding this clone to next upcoming
            '  nodes in actual linked list
            node.[next] = current
        End While
        '  Start to actual linked list
        current = Me.head
        '  Step 2
        '  Set actual value of random field in clone linked list
        while (current IsNot Nothing AndAlso current.[next] IsNot Nothing)
            '  Clone list node
            node = current.[next]
            if (current.random IsNot Nothing) Then
                '  Set random node in clone linked list
                node.random = current.random.[next]
            End If
            '  Visit to actual linked list next node
            current = node.[next]
        End While
        '  Agian start to actual linked list
        current = Me.head
        '  Step 3
        '  Separate actual and clone linked list
        while (current IsNot Nothing)
            node = current.[next]
            if (front  Is  Nothing) Then
                '  Get first node of clone linked list
                front = node
            End If
            current.[next] = node.[next]
            current = current.[next]
            if (current IsNot Nothing) Then
                node.[next] = current.[next]
            Else
                node.[next] = Nothing
            End IF
        End While
        Return front
    End Function
    Public Shared Sub Main(ByVal args As String())
        '  Test linked list
        Dim list1 As MyLinkedList = New MyLinkedList()
        Dim list2 As MyLinkedList = New MyLinkedList()
        '  Create Normal linked list
        list1.insert(5)
        list1.insert(6)
        list1.insert(1)
        list1.insert(8)
        list1.insert(7)
        '   Simple linked list
        '   5 → 6 → 1 → 8 → 7 → NULL
        '   Set random node value
        '   ╷───────────┐
        '   │           │
        '   │   ╷───────│───┐
        '   ↓───│───╷   │   │
        '   ↓   ↓   ↑   │   │
        '   5 → 6 → 1 → 8 → 7 → NULL
        '   │   │       ↑   │
        '   └───│───────┘   │    
        '       │           │
        '       └───────────┘ 
        '    5(8)-->6(7)-->1(5)-->8(5)-->7(6)-->NULL
        '    Linked list with random field
        '  5-->8
        list1.head.random = list1.head.[next].[next].[next]
        '  6-->7
        list1.head.[next].random = list1.head.[next].[next].[next].[next]
        '  1 --> 5
        list1.head.[next].[next].random = list1.head
        '  8 --> 5
        list1.head.[next].[next].[next].random = list1.head
        '  7 --> 6
        list1.head.[next].[next].[next].[next].random = list1.head.[next]
        list2.head = list1.cloneList()
        Console.Write( vbLf &" Actual Linked List")
        list1.display()
        Console.Write( vbLf &" Clone Linked List")
        list2.display()
    End Sub
End Class

Output

 Actual Linked List
 5(8)--->6(7)--->1(5)--->8(5)--->7(6)-->NULL

 Clone Linked List
 5(8)--->6(7)--->1(5)--->8(5)--->7(6)-->NULL




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