Skip to main content

Anticlockwise spiral conversion of doubly linked list in vb.net

Vb program for Anticlockwise spiral conversion of doubly linked list. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net program for
'  Convert a doubly linked list anticlockwise spiral form

'  This is Linked List Node
Public Class LinkNode
    Public  data As Integer
    Public  [next] As LinkNode
    Public  prev As LinkNode
    Public Sub New(ByVal data As Integer)
        Me.data = data
        Me.next = Nothing
        Me.prev = Nothing
    End Sub
End Class
public Class DoublyLinkedList
    Public  head As LinkNode
    Public  tail As LinkNode
    Public Sub New()
        '  Set head and tail
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    '  Insert new node at end position
    Public Sub insert(ByVal value As Integer)
        '  Create a node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            '  Add first node
            Me.head = node
            Me.tail = node
            Return
        End If
        '  Add node at last position
        Me.tail.[next] = node
        node.prev = Me.tail
        Me.tail = node
    End Sub
    '  Display node element of doubly linked list
    Public Sub display()
        if (Me.head  Is  Nothing) Then
            Console.WriteLine("Empty Linked List")
        Else
            Console.Write("Linked List Head to Tail :")
            '  Get first node of linked list
            Dim temp As LinkNode = Me.head
            '  iterate linked list
            while (temp IsNot Nothing)
                '  Display node value
                Console.Write("  " + temp.data.ToString())
                '  Visit to next node
                temp = temp.[next]
            End While
            Console.Write( vbLf &"Linked List Tail to Head :")
            '  Get last node of linked list
            temp = Me.tail
            '  iterate linked list
            while (temp IsNot Nothing)
                '  Display node value
                Console.Write("  " + temp.data.ToString())
                '  Visit to prev node
                temp = temp.prev
            End While
        End IF
    End Sub
    Public Sub spiralTransform()
        if (Me.head  Is  Nothing OrElse 
            Me.head.[next]  Is  Nothing) Then
            Return
        End If
        Dim last As LinkNode = Me.tail
        Dim current As LinkNode = Me.head
        Dim auxiliary As LinkNode = Nothing
        Dim temp As LinkNode = Nothing
        Dim back As LinkNode = Nothing
        '  Set new head
        Me.head = Me.tail
        while (current IsNot Nothing AndAlso last IsNot Nothing)
            if (current  Is  last) Then
                current.[next] = Nothing
                current.prev = back
                Me.tail = current
                '  Set new last node
                Return
            ElseIf (current.[next]  Is  last) Then
                current.[next] = Nothing
                current.prev = last
                last.[next] = current
                last.prev = back
                '  Set new last node
                Me.tail = current
                Return
            End If
            '  Swap link
            auxiliary = current.[next]
            temp = last.prev
            current.prev = last
            last.[next] = current
            current.[next] = temp
            last.prev = back
            back = current
            current = auxiliary
            last = temp
        End While
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim dll As DoublyLinkedList = New DoublyLinkedList()
        '  Insert following linked list nodes
        dll.insert(1)
        dll.insert(2)
        dll.insert(3)
        dll.insert(4)
        dll.insert(5)
        dll.insert(6)
        dll.insert(7)
        dll.insert(8)
        dll.insert(9)
        Console.WriteLine( vbLf &"Before anticlockwise spiral conversion")
        '  Display all node
        dll.display()
        Console.WriteLine( vbLf &"After anticlockwise spiral conversion")
        dll.spiralTransform()
        dll.display()
    End Sub
End Class

Output

Before anticlockwise spiral conversion
Linked List Head to Tail :  1  2  3  4  5  6  7  8  9
Linked List Tail to Head :  9  8  7  6  5  4  3  2  1
After anticlockwise spiral conversion
Linked List Head to Tail :  9  1  8  2  7  3  6  4  5
Linked List Tail to Head :  5  4  6  3  7  2  8  1  9




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