Skip to main content

Effectively insert node at beginning of circular linked list in vb.net

Vb program for Effectively insert node at beginning of circular linked list . Here problem description and other solutions.

' Include namespace system
Imports System 
'  Vb.net Program 
'  Insert node at beginning of circular linked list
'  Using head and tail nodes

'  Define class of linked list Node
Public Class LinkNode 
    Public  data As Integer
    Public  [next] As LinkNode
    
    Public Sub New(ByVal data As Integer, 
                   ByVal first As LinkNode)
        '  Set node value
        Me.data = data
        Me.next = first
    End Sub
End Class
public Class CircularLinkedList 
    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 begining of circular linked list
    Public Sub insert(ByVal value As Integer)
        '  Create a node
        Dim node As LinkNode = New LinkNode(value, Me.head)
        if (Me.tail  Is  Nothing) Then 
            '  First node of linked list
            Me.tail = node
        End If

        '  Make new head
        Me.head = node
        '  Connecting the last node to the top of the next field
        Me.tail.[next] = Me.head
    
    End Sub
    '  Display node element of circular linked list
    
    Public Sub display()
        if (Me.head  Is  Nothing) Then 
            Console.WriteLine("Empty Linked List")
        Else 
            Console.Write("Linked List Element :")
            Dim temp As LinkNode = Me.head
            '  iterate linked list
            
            while (temp IsNot Nothing) 
            
                '  Display node
                Console.Write("  " + temp.data.ToString())
                if (temp  Is  Me.tail) Then 
                    '  Stop iteration
                    '  Or break;
                    Return
                End If

                '  Visit to next node
                temp = temp.[next]
            End While
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim task As CircularLinkedList = New CircularLinkedList()
        '  Add following linked list nodes
        task.insert(8)
        task.insert(7)
        task.insert(6)
        task.insert(5)
        task.insert(4)
        task.insert(3)
        task.insert(2)
        task.insert(1)
        '  Display node
        task.display()
    
    End Sub

End Class

Output

Linked List Element :  1  2  3  4  5  6  7  8




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