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

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

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

'  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)
        
        Me.data = data
        Me.next = first
    
    End Sub

End Class
public Class CircularLinkedList 
    Public  head As LinkNode
    '  Class constructors
    
     Public Sub New()
        
        Me.head = 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.head  Is  Nothing) Then 
            '  First node of linked list
            Me.head = node
            node.[next] = Me.head
        Else 
            Dim temp As LinkNode = Me.head
            '  Find the last node 
            while (temp.[next] IsNot Me.head) 
                '  Visit to next node
                temp = temp.[next]
            End While
            '  Add node
            temp.[next] = node
            '  make new head node
            Me.head = node
        End IF

    
    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.WriteLine("  Linked List Element : ")
            '  Get first node
            Dim temp As LinkNode = Me.head
            '  iterate linked list
            while (temp IsNot Nothing) 
                '  Display node
                Console.Write("  " + temp.data.ToString())
                '  Visit to next node
                temp = temp.[next]
                if (temp  Is  Me.head) Then 
                    '  Stop iteration
                    Return
                End If

            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


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







© 2021, kalkicode.com, All rights reserved