Skip to main content

Find the smallest number in the doubly linked list in vb.net

Vb program for Find the smallest number in the doubly linked list. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net program for
'  Find the smallest node in doubly linked list

'  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 inital value
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    
    '  Insert new node at end position
    Public Sub insert(ByVal value As Integer)
        '  Create a 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
        '  new last node
        Me.tail = node
    End Sub
    
    '  Find smallest number
    Public Sub minNumber()
        if (Me.head  Is  Nothing) Then
            Console.WriteLine("Empty Linked List")
        Else
            '  Get first node of linked list
            Dim temp As LinkNode = Me.head
            Dim result As Integer = temp.data
            '  iterate linked list
            while (temp IsNot Nothing)
                if (temp.data < result) Then
                    '  Get new minimum
                    result = temp.data
                End If
                '  Visit to next node
                temp = temp.[next]
            End While
            Console.WriteLine("Smallest : " + result.ToString())
        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(14)
        dll.insert(31)
        dll.insert(12)
        dll.insert(15)
        dll.insert(11)
        dll.insert(25)
        dll.minNumber()
    End Sub
End Class

Output

Smallest : 11




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