Skip to main content

Insertion at end of circular doubly linked list in vb.net

Vb program for Insertion at end of circular doubly linked list. Here more information.

' Include namespace system
Imports System 
'  Vb.net Program 
'  Insert node at end of circular doubly linked list

'  Define class of 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)
        '  Set node value
        Me.data = data
        Me.next = Nothing
        Me.prev = Nothing
    End Sub

End Class
public Class CircularDLL 
    Public  head As LinkNode
    Public  tail As LinkNode
    
    Public Sub New()
        '  Set head and tail values
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    
    '  Insert node at end of circular doubly linked list
    Public Sub insert(ByVal value As Integer)
        '  Create a node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then 
            '  First node of linked list
            Me.head = node
            Me.tail = node
            node.[next] = node
            node.prev = node
        Else 
            node.[next] = Me.head
            node.prev = Me.tail
            Me.head.prev = node
            Me.tail.[next] = node
            '  Set new last node
            Me.tail = node
        End IF

    
    End Sub
    
    Public Sub headToTail()
        if (Me.head  Is  Nothing) Then 
            Console.WriteLine("Empty linked list")
        Else 
            Dim temp As LinkNode = Me.head
            Console.WriteLine( vbLf &"Node Form Front to Rear :")
            
            while (temp IsNot Nothing) 
            
                Console.Write("  " + temp.data.ToString())
                temp = temp.[next]
                if (temp  Is  Me.head) Then 
                    Return
                End If

            End While
        End IF

    
    End Sub
    
    Public Sub tailToHead()
        if (Me.tail  Is  Nothing) Then 
            Console.Write("Empty linked list")
        Else 
            Dim temp As LinkNode = Me.tail
            Console.WriteLine( vbLf &"Node Form Rear to Front :")
            
            while (temp IsNot Nothing) 
            
                Console.Write("  " + temp.data.ToString())
                
                temp = temp.prev
                if (temp  Is  Me.tail) Then 
                    Return
                End If

            End While
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim cdll As CircularDLL = New CircularDLL()
        '  Add following linked list nodes
        cdll.insert(1)
        cdll.insert(2)
        cdll.insert(3)
        cdll.insert(4)
        cdll.insert(5)
        cdll.insert(6)
        '  Display node
        cdll.headToTail()
        cdll.tailToHead()
    
    End Sub

End Class

Output

Node Form Front to Rear :
  1  2  3  4  5  6
Node Form Rear to Front :
  6  5  4  3  2  1




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