Skip to main content

Subtraction of alternate nodes of linked list in vb.net

Vb program for Subtraction of alternate nodes of linked list. Here problem description and other solutions.

' Include namespace system
Imports System 
'    Vb.net program for
'    Subtraction of the alternate nodes of linked list

'  Linked list node
Public Class LinkNode
    Public  data As Integer
    Public  [next] As LinkNode
    Public Sub New(ByVal data As Integer)
        Me.data = data
        Me.next = Nothing
    End Sub
End Class

public Class SingleLL
    Public  head As LinkNode
    Public  tail As LinkNode
    Public Sub New()
        '  Set initial value
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    Public Sub insert(ByVal data As Integer)
        Dim node As LinkNode = New LinkNode(data)
        if (Me.head  Is  Nothing) Then
            '  Add first node
            Me.head = node
        Else
            '  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
            Return
        End If
        Dim temp As LinkNode = Me.head
        '  iterating linked list elements
        while (temp IsNot Nothing)
            Console.Write(" " + temp.data.ToString() + " →")
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.WriteLine(" NULL")
    End Sub
    '  Find the subtraction of all alternate 
    '  nodes in linked list
    Public Sub alternateSubtraction()
        '  Define resultant variables
        Dim result As Integer = 0
        Dim counter As Integer = 0
        '  Start to first node of linked list
        Dim temp As LinkNode = Me.head
        '  iterating linked list elements
        while (temp IsNot Nothing)
            if (counter Mod 2 = 0) Then
                '  When get alternate node
                if (result = 0) Then
                    result = temp.data
                Else
                    result = result - temp.data
                End IF
            End If
            '  Node counter
            counter += 1
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.WriteLine(" Alternate nodes subtraction : " + 
                          result.ToString())
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        '  Add node in linked list
        '  4 → 7 → 2 → 9 → 1 → 3 → 4 → 3 → 6 → NULL
        sll.insert(4)
        sll.insert(7)
        sll.insert(2)
        sll.insert(9)
        sll.insert(1)
        sll.insert(3)
        sll.insert(4)
        sll.insert(3)
        sll.insert(6)
        '  Display of linked list nodes
        sll.display()
        '  Test
        sll.alternateSubtraction()
    End Sub
End Class

Output

 4 → 7 → 2 → 9 → 1 → 3 → 4 → 3 → 6 → NULL
 Alternate nodes subtraction : -9




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