Skip to main content

Rotate linked list clockwise by k nodes in vb.net

Rotating linked list node clockwise

Vb program for Rotate linked list clockwise by k nodes. Here more solutions.

' Include namespace system
Imports System 
'  Vb.net program for 
'  Rotate a list by moving each element 
'  k times to the right.
'  Or clockwise rotation of linked list 

'  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
            Console.WriteLine( vbLf &" Empty linked list")
            Return
        End If
        Dim temp As LinkNode = Me.head
        '  iterating linked list elements
        while (temp IsNot Nothing)
            Console.Write(" {0} →",temp.data)
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.WriteLine(" NULL")
    End Sub
    
    '  This are perform the linked list rotation
    Public Sub rotation(ByVal k As Integer)
        '  Define some auxiliary variable
        Dim auxiliary As LinkNode = Me.head
        Dim count As Integer = 0
        '  Count number of node in linked list
        while (auxiliary IsNot Nothing)
            '  visit to next node
            auxiliary = auxiliary.[next]
            count += 1
        End While
        if (count <= 1) Then
            '  When have less than 2 nodes in linked list
            Return
        End If
        '  Get actual rotation
        count = k Mod count
        if (count = 0) Then
            '  When actual linked list are not affected
            Return
        End If
        auxiliary = Me.head
        '  Find the rotational node
        while (count > 1)
            '  Visit to next node
            auxiliary = auxiliary.[next]
            count -= 1
        End While
        '  Connecting the last node to first node of linked list
        Me.tail.[next] = Me.head
        '  Set new last node
        Me.tail = auxiliary
        '  Set new head
        Me.head = auxiliary.[next]
        '  Set that there is no node after of tail
        Me.tail.[next] = Nothing
    End Sub
    
    '  Handles the request to perform rotation
    '  k is indicate node rotation
    Public Sub clockwiseRotation(ByVal k As Integer)
        if (k <= 0) Then
            '  When invalid given k
            Return
        End If
        if (Me.head  Is  Nothing) Then
            Console.WriteLine( vbLf &" Empty Linked List")
            Return
        End If
        '  Display given linked list
        Console.WriteLine(" Before rotate ")
        Console.WriteLine(" Linked List : ")
        Me.display()
        Console.WriteLine(" Given k : {0}", k)
        '  Perform rotation
        Me.rotation(k)
        '  Display resultant linked list
        Console.WriteLine(" After rotate ")
        Console.WriteLine(" Linked List : ")
        Me.display()
        Console.WriteLine( vbLf )
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim sll1 As SingleLL = New SingleLL()
        Dim sll2 As SingleLL = New SingleLL()
        '  First linked list
        '  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
        sll1.addNode(1)
        sll1.addNode(2)
        sll1.addNode(3)
        sll1.addNode(4)
        sll1.addNode(5)
        sll1.addNode(6)
        sll1.addNode(7)
        sll1.addNode(8)
        '  Second linked list
        '  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
        sll2.addNode(4)
        sll2.addNode(9)
        sll2.addNode(7)
        sll2.addNode(3)
        sll2.addNode(8)
        sll2.addNode(6)
        sll2.addNode(-2)
        '  Test case
        sll1.clockwiseRotation(3)
        sll2.clockwiseRotation(18)
    End Sub
End Class

Output

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL


 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → 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