Skip to main content

Sum of smaller elements of nodes in a linked list in

Vb program for Sum of smaller elements of nodes in a linked list. Here problem description and other solutions.

' Include namespace system
Imports System 
' program for
' Sum of smallest element of linked list

'  Linked list node
Public Class LinkNode
    Public  a As Integer
    Public  b As Integer
    Public  [next] As LinkNode
    Public Sub New(ByVal a As Integer, 
                   ByVal b As Integer)
        Me.a = a
        Me.b = b = Nothing
    End Sub
End Class
public Class SingleLL
    Public  head As LinkNode
    Public  tail As LinkNode
    Public Sub New()
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    Public Sub insert(ByVal a As Integer, 
                      ByVal b As Integer)
        Dim node As LinkNode = New LinkNode(a, b)
        if (Me.head  Is  Nothing) Then
            '  Add first node
            Me.head = node
            '  Add node at the end position
            Me.tail.[next] = node
        End IF
        '  new last node
        Me.tail = node
    End Sub
    '  Display linked list element
    Public Sub display()
        if (Me.head  Is  Nothing) Then
        End If
        Dim temp As LinkNode = Me.head
        '  iterating linked list elements
        while (temp IsNot Nothing)
            Console.Write(" (" + temp.a.ToString() + 
                          "," + temp.b.ToString() + ") →")
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.WriteLine(" NULL")
    End Sub
    '  Find the sum of smaller elements of 
    '  every node in a linked list
    Public Function  nodeSum() As Integer
        Dim result As Integer = 0
        Dim temp As LinkNode = Me.head
        if (temp  Is  Nothing) Then
            Console.WriteLine("Empty linked list")
            '  Iterate the linked list
            '  And 
            '  Sum of smallest key in every node of linked list
            while (temp IsNot Nothing)
                if (temp.a > temp.b) Then
                    '  When key b is small      
                    result += temp.b
                    '  When key a is small     
                    result += temp.a
                End IF
                '  Visit to next node
                temp = temp.[next]
            End While
        End IF
        Return  result
    End Function
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        '  Add pair
        '  (3,9) → (11,4) → (3,3) → (6,1) → (2,9) → (4,7) → NULL
        sll.insert(3, 9)
        sll.insert(11, 4)
        sll.insert(3, 3)
        sll.insert(6, 1)
        sll.insert(2, 9)
        sll.insert(4, 7)
        Console.WriteLine(" Linked List")
        Console.WriteLine(" Result : " + sll.nodeSum().ToString())
    End Sub
End Class


 Linked List
 (3,9) → (11,4) → (3,3) → (6,1) → (2,9) → (4,7) → NULL
 Result : 17


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