Posted on by Kalkicode
Code Doubly linked list

Find the length of doubly linked list in vb.net

Vb program for Find the length of doubly linked list. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net program for
'  Find length 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  tail As LinkNode
    
    Public Sub New()
        '  Set head and tail
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    
    '  Insert new node at end position
    Public Sub insert(ByVal value As Integer)
        '  Create a new node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then 
            '  Add first node
            Me.head = node
            Me.tail = node
            Return
        End If

        '  Add node at last position
        Me.tail.[next] = node
        node.prev = Me.tail
        Me.tail = node
    
    End Sub
    
    Public Function  length() As Integer
        Dim size As Integer = 0
        '  Get first node of linked list
        Dim temp As LinkNode = Me.head
        
		'  iterate linked list 
        while (temp IsNot Nothing) 
            '  Increase the size by one
            size += 1
            '  Visit to next node
            temp = temp.[next]
        End While
        Return size
    
    End Function 
    
    Public Shared Sub Main(ByVal args As String())
        Dim dll As DoublyLinkedList = New DoublyLinkedList()
        '  Insert following linked list nodes
        dll.insert(4)
        dll.insert(3)
        dll.insert(2)
        dll.insert(5)
        dll.insert(3)
        Console.WriteLine("Length  : " + dll.length().ToString())
    
    End Sub

End Class

Output

Length  : 5

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