Insert node at beginning of doubly linked list in vb.net

Vb program for Insert node at beginning of doubly linked list. Here problem description and other solutions.

' Include namespace system
Imports System 
'  Vb.net Program For
'  insert new node at beginning of 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)
        Me.data = data
        Me.next = Nothing
        Me.prev = Nothing
    
    End Sub

End Class
public Class DoublyLinkedList 
    Public  head As LinkNode
    
    Public Sub New()
        '  Set inital value
        Me.head = Nothing
    End Sub
    
    '  Insert new node at beginning position
    Public Sub insert(ByVal value As Integer)
        '  Create a node
        Dim node As LinkNode = New LinkNode(value)
        node.[next] = Me.head
        '  When linked list is not empty
        if (Me.head IsNot Nothing) Then 
            Me.head.prev = node
        End If
        '  Make new head
        Me.head = 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.WriteLine("  Doubly Linked List Element :")
            '  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
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim dll As DoublyLinkedList = New DoublyLinkedList()
        '  Insert following linked list nodes
        dll.insert(70)
        dll.insert(60)
        dll.insert(50)
        dll.insert(40)
        dll.insert(30)
        dll.insert(20)
        dll.insert(10)
        '   NULL <- 10 <--> 20 <--> 30 <--> 40 <--> 50 <--> 60 <--> 70->NULL
        dll.display()
    
    End Sub

End Class

Output

  Doubly Linked List Element :
  10  20  30  40  50  60  70


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