Posted on by Kalkicode
Code Single linked list

Reverse initial k elements from linked list in vb.net

Vb program for Reverse initial k elements from linked list. Here more information.

' Include namespace system
Imports System 
'  Vb.net program for
'  Reverse first K elements of given linked list

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

public Class SingleLL
    Public  head As LinkNode
    Public  tail As LinkNode
    Public Sub New()
        '  Set head and tail 
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    '  Add new node at the end of linked list
    Public Sub insert(ByVal value As Integer)
        '  Create a new node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            Me.head = node
        Else
            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
    '  Check whether k node exists in linked list
    Public Function  checkLength(ByVal k As Integer) As Boolean
        Dim counter As Integer = 0
        Dim auxiliary As LinkNode = Me.head
        while (auxiliary IsNot Nothing AndAlso counter < k)
            '  Visit to next node
            auxiliary = auxiliary.[next]
            counter += 1
        End While
        Return counter = k
    End Function
    Public Sub reverse(ByVal k As Integer)
        if (Me.head IsNot Nothing AndAlso k > 1) Then
            '  Test reverse node exists or not
            if (Me.checkLength(k)) Then
                '  When k is valid
                Console.WriteLine("Reverse " + 
                                  k.ToString() + " elements")
                '  Some auxiliary variable which is using to 
                '  perform reverse operation
                Dim auxiliary As LinkNode = Me.head
                Dim back As LinkNode = Nothing
                Dim temp As LinkNode = Me.head
                '  Reverse initial k nodes
                while (auxiliary IsNot Nothing AndAlso k > 0)
                    '  Visit to next node
                    temp = temp.[next]
                    auxiliary.[next] = back
                    back = auxiliary
                    auxiliary = temp
                    '  Reduce counter
                    k -= 1
                End While
                if (auxiliary  Is  Nothing) Then
                    '  When reverse all nodes
                    Me.tail = Me.head
                End If
                Me.head.[next] = auxiliary
                '  Set new head node
                Me.head = back
            Else
                Console.WriteLine( vbLf &"Inital " + 
                                  k.ToString() + 
                                  " nodes are not exist")
            End IF
        End If
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        Dim k As Integer = 5
        '  Add linked list node
        sll.insert(1)
        sll.insert(2)
        sll.insert(3)
        sll.insert(4)
        sll.insert(5)
        sll.insert(6)
        sll.insert(7)
        '  Before effect
        Console.WriteLine("Before reverse " + 
                          k.ToString() + " nodes")
        '  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
        sll.display()
        '  Perform reverse operation
        sll.reverse(k)
        '  After effect
        Console.WriteLine("After reverse initial " + 
                          k.ToString() + " nodes")
        '  5 → 4 → 3 → 2 → 1 → 6 → 7 → NULL
        sll.display()
    End Sub
End Class

Output

Before reverse 5 nodes
1 → 2 → 3 → 4 → 5 → 6 → 7 → null
Reverse 5 elements
After reverse initial 5 nodes
5 → 4 → 3 → 2 → 1 → 6 → 7 → 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