Skip to main content

Insert node at beginning of doubly linked list in

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

' Include namespace system
Imports System 
' 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) = data = 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")
            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("  " +
                '  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
        '   NULL <- 10 <--> 20 <--> 30 <--> 40 <--> 50 <--> 60 <--> 70->NULL
    End Sub

End Class


  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