Insert node at beginning of linked list in vb.net

insert node at beginning of linked list

Vb program for Insert node at beginning of linked list. Here mentioned other language solution.

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

'  Linked list node
Public Class LinkNode
    Public  data As Integer
    Public  [next] As LinkNode
    Public Sub New(ByVal data As Integer)
        Me.data = data
        Me.next = Nothing
    End Sub
End Class

public Class SingleLL
    Public  head As LinkNode
    Public Sub New()
        Me.head = Nothing
    End Sub
    '  Adding new node at beginning of linked list
    Public Sub addNode(ByVal data As Integer)
        '  Create new node
        Dim node As LinkNode = New LinkNode(data)
        '  Connect current node to previous head
        node.[next] = Me.head
        Me.head = 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(" {0} →", temp.data)
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.Write(" NULL"& vbLf )
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        '  Linked list
        '  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
        sll.addNode(8)
        sll.addNode(7)
        sll.addNode(6)
        sll.addNode(5)
        sll.addNode(4)
        sll.addNode(3)
        sll.addNode(2)
        sll.addNode(1)
        sll.display()
    End Sub
End Class

Output

 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 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







© 2022, kalkicode.com, All rights reserved